java - Spring AOP Autowiring 异常

标签 java spring spring-aop autowired

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

相关文章:

java - 货币异常 : No MonetaryAmountsSingletonSpi loaded

java - spring mvc拦截器如何获取方法执行流程

java - 如何将 AspectJ 加载时编织与 Spring AOP 结合使用?

java - 创建文档比较软件

java - Apache Camel : Aws-S3 consumer starts failing with connection pool timeout

java - Spring:从 Gmail 发送电子邮件

java - Spring AOP 有条件地@DeclareParents

java - 将 Java 加密算法移植到 C++ OpenSSL

java - 汉诺塔与 Java 栈

spring - 无法使用 javaconfig 解析名称为 'htmlviews/index.html' 的 servlet 中名称为 'dispatcher' 的 View