jscherman
jscherman

Reputation: 6189

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. Look, i have this client class:

package com.example.aspects;

public class Client {


    public void talk(){

    }
}

And my aspect: package 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

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. have you any idea about what is going on ? Regards

Upvotes: 0

Views: 542

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279960

You never registered your @Aspect. Add a corresponding 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.

Upvotes: 2

Related Questions