首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@EnableAspectJAutoProxy不适用于proxyTargetClass=false

@EnableAspectJAutoProxy不适用于proxyTargetClass=false
EN

Stack Overflow用户
提问于 2014-02-03 03:03:35
回答 2查看 6.3K关注 0票数 5

我是第一次学习Spring AOP。

我在这个网站上读到:Site2Site1

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

主类:

代码语言:javascript
复制
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());
    }
}

应用配置类:

代码语言:javascript
复制
@Configuration
@ComponentScan("com.pjcom.springaop")
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class AppConfig {

    @PostConstruct
    public void doAlert() {

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

}

Aspect类:

代码语言:javascript
复制
@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。然后我得到下面的错误。

代码语言:javascript
复制
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)

为什么?

EN

回答 2

Stack Overflow用户

发布于 2016-07-04 06:00:35

代码语言:javascript
复制
@EnableAspectJAutoProxy(proxyTargetClass=false)

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

代码语言:javascript
复制
@EnableAspectJAutoProxy(proxyTargetClass=true)

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

票数 3
EN

Stack Overflow用户

发布于 2017-10-26 14:26:25

必须将1>消息打印机定义为组件,即:`

代码语言:javascript
复制
 package com.pjcom.springaop.message.impl;
    @Component
    public class MessagePrinter{
    public void getMessage(){
    System.out.println("getMessage() called");
    }
    }`

在与配置java文件相同的包中,如果没有为其他包定义@ComponentScan。

2>如果相同类型的bean类有许多其他依赖项,那么要在spring Config中解析依赖项,请使用@Qualifier注解。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21514858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档