gpt4 book ai didi

java - Spring AOP Autowiring 异常

转载 作者:行者123 更新时间:2023-11-30 07:20:59 25 4
gpt4 key购买 nike

我在我的项目中使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com