Introspector
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public class IntrospectorDemo { public static void main(String[] args) {
// TODO Auto-generated method stub
IntrospectorDemo id=new IntrospectorDemo();
try {
id.test1();
id.test2();
id.test3();
id.test4();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public class Person
{
public Person(){
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} } //获得person类的所有bean属性 public void test1() throws Exception{
BeanInfo info = Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] pds = info.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
System.out.println(pd.getPropertyType()+" "+pd.getName());
}
}
//获得Person(不包含Object类)的所有bean属性 public void test2() throws Exception{
BeanInfo info = Introspector.getBeanInfo(Person.class,Object.class);
PropertyDescriptor[] pds = info.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
System.out.println(pd.getPropertyType()+" "+pd.getName());
}
}
//设置Person的age属性 public void test3() throws Exception{
Person p = new Person();
PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
Method method = pd.getWriteMethod();
method.invoke(p, 20);
System.out.println(p.getAge());
}
//调用Person的getAge() public void test4() throws Exception{
Person p = new Person("xiazdong",30);
PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
Method method = pd.getReadMethod();
System.out.println(method.invoke(p, null));
} }
Introspector的更多相关文章
- 解决java.beans.Introspector导致的内存泄漏
解决方案: 在WEB.XML 中配置监听器: <listener> <listener-class> org.springframework.web.util.Introspe ...
- 内省(introspector)------>JavaBean
内省(introspector)------>JavaBean 1.问什么要学内省? 开发框架时,经常需要Java对象的属性来来封装程序的数据,每次使用反射技术完成此操作过于 ...
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
本文转载自:http://blog.sina.com.cn/s/blog_5d65a16901011kom.html 关键字:内省.JavaBean.PropertyDescriptor类.Int ...
- 内省(Introspector)
内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法 目的:主要用于传递数据信息,这种类中的方法主要用于访问私有的字段(且方法名符合某种命名规则) p ...
- [新手学Java]使用内省(Introspector)操作JavaBean属性
获取类bean中的所有属性: @Test //获取类bean中的所有属性 public void test1() throws Exception{ BeanInfo info = Introspec ...
- JAVA内省(Introspector)
什么是Java内省:内省是Java语言对Bean类属性.事件的一种缺省处理方法. Java内省的作用:一般在开发框架时,当需要操作一个JavaBean时,如果一直用反射来操作,显得很麻烦:所以sun公 ...
- java的内省(introspector)
package com.wzh.test.introspector; import java.beans.BeanInfo; import java.beans.IntrospectionExcept ...
- java内省机制Introspector
访问JavaBean属性的两种方式 1)直接调用bean的setXXX或getXXX方法: 2)通过内省技术访问(java.beans包提供了内省的API),内省技术访问也提供了两种方式: a)通过P ...
- Introspector(内省)简单演示样例 与 简单应用
简单演示样例: package com.asdfLeftHand.test; import java.beans.BeanDescriptor; import java.beans.BeanInfo; ...
- Java 内省 Introspector
操纵类的属性,有两种方法 反射 内省 面向对象的编程中,对于用户提交过来的数据,要封装成一个javaBean,也就是对象 其中Bean的属性不是由字段来决定的,而是由get和Set方法来决定的 pub ...
随机推荐
- 紫书 例题8-11 UVa 10954 (优先队列)
解法和合并果子是一样的, 每次取最小的两个, 更新答案, 加入队列 #include<cstdio> #include<queue> #define REP(i, a, b) ...
- 紫书 习题 11-15 UVa 1668 (图论构造法)
参考了http://www.bubuko.com/infodetail-1276416.html 首先是逆向思维, 向把每条边看作一条路径, 然后再去合并 然后我们讨论怎么样合并时最优的 我们讨论当前 ...
- 2015 Multi-University Training Contest 2 hdu 5306 Gorgeous Sequence
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- SpringBoot之通过Maven将项目打包成ROOT.war-yellowcong
在项目中,我们通过maven的插件,将项目达成war包,然后通过jenkins,自动化部署项目. 核心的maven配置文件,下面这一段pom.xml的配置文件. 将项目打包成ROOT.war < ...
- An existing connection was forcibly closed by the remote host
StackOverflow https://stackoverflow.com/questions/5420656/unable-to-read-data-from-the-transport-con ...
- 前端到后台ThinkPHP开发整站--php开发案例
前端到后台ThinkPHP开发整站--php开发案例 总结 还是需要做几个案例,一天一个为佳,那样才能做得快. 从需求分析着手,任务体系要构建好,这样才能非常高效. 转自: 前端到后台ThinkPHP ...
- [luogu P2586] GCD 解题报告 (莫比乌斯反演|欧拉函数)
题目链接:https://www.luogu.org/problemnew/show/P2568#sub 题目大意: 计算$\sum_{x=1}^n\sum_{y=1}^n [gcd(x,y)==p ...
- [ Java ] [ Eclipse ] content Auto activation triggers
重點: That plug-in is not necessary any more. Just go to Preferences > Java > Editor > Conten ...
- springMVC中跳转问题
在使用SpringMVC时遇到了这个跳转的问题很头疼.现在总结出来,对以后的开发有所帮助. . 1.可以采用ModelAndView: @RequestMapping("test1" ...
- JS动态创建表单post提交
<script> //@创建表单方法 function post(URL, PARAMS) { var temp = document.createElement("form&q ...