我在我的项目中使用 AspectJ。
@Aspect
Class FooAsspectj{
private static Logger log = LoggerFactory.getLogger(FooAsspectj.class);
@Pointcut("execution(* com.abc.Foo.getFoo(..))")
public void getFoo() { }
@Around("getFoo()")
public Object profileFoo(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
log.debug("Going to call the method.");
Object output = pjp.proceed();
log.debug("Method execution completed.");
long elapsedTime = System.currentTimeMillis() - start;
log.debug("Method execution time: " + elapsedTime + " milliseconds.");
return output;
}
}
Class Foo{
public void getFoo(String abc){
System.out.println("Hello Foo");
}
现在我有另一个类 UseFoo,我在其中 Autowiring Foo 类
Class UseFoo{
@Autowired
private Foo foo;
}
我在 ApplicationContext.xml 中做了以下条目
<aop:aspectj-autoproxy />
<bean id="fooaspect" class="FooAsspectj" />
我收到错误
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'useFoo': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private Foo UseFoo.foo; nested exception is java.lang.IllegalArgumentException: Can not set Foo field UseFoo.foo to $Proxy56
如果我删除 <bean id="fooaspect" class="FooAsspectj" />
效果很好。
最佳答案
使用以下方法可以解决此问题
<aop:aspectj-autoproxy proxy-target-class="true"/>
关于java - Spring AOP Autowiring 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13642701/