简体   繁体   English

Spring 4 AOP方面从未被称为?

[英]Spring 4 AOP aspect is never being called?

I am using Spring 4 AOP and the aspect that i create is never being called and i cannot figure it out why is that. 我正在使用Spring 4 AOP,而我创建的方面从未被调用过,而且我无法弄清楚为什么会这样。 Look, i have this client class: 看,我有这个客户端类:

package com.example.aspects;

public class Client {


    public void talk(){

    }
}

And my aspect: package com.example.aspects; 我的方面是:包com.example.aspects;

import org.aspectj.lang.JoinPoint;
@Aspect
public class AspectTest {

    @Before("execution(* com.example.aspects.Client.talk(..))") 
    public void doBefore(JoinPoint joinPoint) {
        System.out.println("***AspectJ*** DoBefore() is running!! intercepted : " + joinPoint.getSignature().getName());
    }

}

My configuration file: 我的配置文件:

package com.example.aspects;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
public class Config {

    @Bean
    public Client client(){
        return new Client();
    }

}

And finally, de app 最后,de app

public class App {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(
                Config.class);
        Client client = (Client) appContext.getBean("client");
        client.talk();
    }
}

By this way, i never get "intercepted" by AspectTest doBefore() method. 通过这种方式,我永远不会被AspectTest doBefore()方法“拦截”。 have you any idea about what is going on ? 您知道发生了什么吗? Regards 问候

You never registered your @Aspect . 您从未注册过@Aspect Add a corresponding bean 添加一个对应的bean

@Bean
public AspectTest aspect() {
    return new AspectTest();
}

You can also make your type a @Component and add an appropriate @ComponentScan in your @Configuration class. 您也可以将类型设置为@Component并在@Configuration类中添加适当的@ComponentScan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM