public class abc{
@UserInfo//自定义的注解
private UserInfo userInfo;
@Autowired
private UserInfoDao dao;
public void save (){
dao.save(userInfo);
}
}
//我定义一个注解,用aop在save方法执行前拦截save方法然后给userInfo赋值,但是一直赋值不了
//用下面这种不行,field就是类属性userInfo,用它的set方法成功赋值之后,这边也是没值的
//field.set(cls.newInstance(), field.getType().newInstance());
//下面这种也不行,
//Method method = cls.getMethod("set" + name, UserInfo.class);
//method.invoke(cls.newInstance(), field.getType().newInstance());
//根本原因是cls.newInstance()这个是新对象,不是原来被拦截的方法的时候的那个类对象,所以,在拦
//截器里面怎么获得被拦截的方法的类对象????或者要怎么操作才能赋值???
如上述所示要怎么在拦截器里给userInfo赋值,类似于spring 的 @Autowired注解,有无大佬提点一下。
@Around("pointcut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object target = proceedingJoinPoint.getTarget();
if (target instanceof abc) {
// do sth
}
return null;
}
终于自己找到方法了
applicationContext.getBean(className)就可以获得被拦截的方法的类对象了,解决了