@EnableAspectJAutoProxy不使用proxyTargetClass=FALSE [英] @EnableAspectJAutoProxy not work with proxyTargetClass=false

查看:0
本文介绍了@EnableAspectJAutoProxy不使用proxyTargetClass=FALSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次了解Spring AOP。

我在这个网站上读到: Site2Site1

在此之后,我进行了下面的课程

主类:

public class App {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(AppConfig.class);
        context.refresh();

        MessagePrinter printer = context.getBean(MessagePrinter.class);

        System.out.println(printer.getMessage());
    }
}

应用配置类:

@Configuration
@ComponentScan("com.pjcom.springaop")
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class AppConfig {

    @PostConstruct
    public void doAlert() {

        System.out.println("Application done.");
    }

}

特征类:

@Component
@Aspect
public class AspectMonitor {

    @Before("execution(* com.pjcom.springaop.message.impl.MessagePrinter.getMessage(..))")
    public void beforeMessagePointCut(JoinPoint joinPoint) {

        System.out.println("Monitorizando Mensaje.");
    }

}

和其他...

就像该应用程序一样工作得很好,但如果我将proxyTargetClass设置为FALSE。然后我得到下面的错误。

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pjcom.springaop.message.impl.MessagePrinter] is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:318)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)
    at com.pjcom.springaop.App.main(App.java:18)

为什么?

推荐答案

@EnableAspectJAutoProxy(proxyTargetClass=false)

表示将创建JDK动态代理以支持对象上的方面执行。因此,由于此类型的代理需要一个类来实现接口,因此您的MessagePrinter必须实现某个声明方法getMessage的接口。

@EnableAspectJAutoProxy(proxyTargetClass=true)

相反,指示使用CGLIB代理,它能够为没有接口的类创建代理。

这篇关于@EnableAspectJAutoProxy不使用proxyTargetClass=FALSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆