通过注解注入对象使用.annotationType().getDeclaredMethods()得到

Person类
package com.introspector;

public class Person {
private String name;
private int age;
private boolean gender;

public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
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;
}

}


PersonDao类

package com.introspector;

public class PersonDao {
private Person person;

public Person getPerson() {
return person;
}

@Injection(name="张一", age=11, gender=false)
public void setPerson(Person person){
this.person = person;
}
}


Injection类


package com.introspector;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Injection {

String name();

int age();

boolean gender() default true;

}



TestDemo类


//得到PersonDao上person的属性描述器
PropertyDescriptor pd = new PropertyDescriptor("person", PersonDao.class);
Class clazz = pd.getPropertyType();
Object p = clazz.newInstance();
Method writeMethod = pd.getWriteMethod();

Injection injection = writeMethod.getAnnotation(Injection.class);
//injection.annotationType返回的是interface com.introspector.Injection 接口再直接得到注解接口对应的方法
Method[] methods = injection.annotationType().getDeclaredMethods();
Field f = null;
for(Method m : methods){
//直接往person的属性中注入
f = p.getClass().getDeclaredField(m.getName());
f.setAccessible(true);
f.set(p, m.invoke(injection, null));
}

PersonDao pdo = new PersonDao();
writeMethod.invoke(pdo, p);
System.out.println(pdo.getPerson().getAge());
System.out.println(pdo.getPerson().getName());
System.out.println(pdo.getPerson().isGender());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Caused by: java.lang.NoClassDefFoundError: com/venustech/ca/system/model/SoftwareUpgradeBO at java.lang.Class.getDeclaredMethods0(Native Method) ~[?:1.8.0_231] at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[?:1.8.0_231] at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[?:1.8.0_231] at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:467) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:267) ~[spring-beans-5.3.23.jar:5.3.23] ... 27 more Caused by: java.lang.ClassNotFoundException: com.venustech.ca.system.model.SoftwareUpgradeBO at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1358) ~[catalina.jar:8.5.54] at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1180) ~[catalina.jar:8.5.54] at java.lang.Class.getDeclaredMethods0(Native Method) ~[?:1.8.0_231] at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[?:1.8.0_231] at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[?:1.8.0_231] at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:467) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321) ~[spring-core-5.3.23.jar:5.3.23] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:267) ~[spring-beans-5.3.23.jar:5.3.23]
最新发布
06-03

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值