Michael Coxon
Michael Coxon

Reputation: 3535

AOP with Spring Boot Advice Not Triggering

Using: Spring Boot :: (v1.2.8.RELEASE)

I have set up a Spring Boot application with the aop starter in build.gradle

compile("org.springframework.boot:spring-boot-starter-aop")

I have checked and I am getting the dependencies:

|    |    |    |         +--- org.springframework:spring-aop:4.1.9.RELEASE
|    |    |    |         |    +--- aopalliance:aopalliance:1.0

This is the AspectConfig:

@Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class AspectConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

I have placed the Configuration class at the base of the application hierarchy, so that the component scanning just covers the whole application. This is all prototype code,but it will eventually form part of a starter module, the the ability to scan all areas would be helpful.

Now I have defined an annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface AutowiredRestTemplate {
    String name();
    String methodUrl();
}

And have a test method:

@Component(value = "testGateway")
public class TestGatewayImpl implements TestGateway {
    private static final Logger LOG = LoggerFactory.getLogger(TestGatewayImpl.class);

    AuspostRestTemplate restTemplate;

    @AutowiredRestTemplate(name = "locations", methodUrl = "/fishing")
    public Response doWork() {
        LOG.debug("Got into gateway with restTemplate {}", restTemplate);
        return restTemplate.getForObject(Response.class);
    }
}

and now the advice:

@Aspect
@Component
public class AutowiredRestTemplateAspect {

    @Autowired
    Map<String, AuspostRestTemplate> restTemplateMap;

    @Autowired
    private ApplicationContext context;

    @Pointcut("execution(public * *(..))")
    public void anyPublicMethod(){}

    @Around("anyPublicMethod() && @annotation(autowiredRestTemplate)")
    public Object inAnyMethod(ProceedingJoinPoint pjp, AutowiredRestTemplate autowiredRestTemplate) throws Throwable{

        AuspostRestTemplate restTemplate = restTemplateMap.get(autowiredRestTemplate.name());
        restTemplate.setMethodUrl(autowiredRestTemplate.methodUrl());
            pjp.getTarget().getClass().getDeclaredField("restTemplate").set(pjp.getTarget(),restTemplate);
        return pjp.proceed();

    }
}

The issue is that the Advice never gets triggered when the doWork() method is run. It even looks as though from the logs that the pointcut doesn't even get set up. Can anyone see what's wrong here?

EDIT: I have added the Config and the Retention and Target annotations for the Annotation that I want to use (above in this question). EDIT2: Changed the ComponentScan on the Configuration class as the other thing was complicated and didn't work anyway.

Upvotes: 1

Views: 2182

Answers (1)

Jakub Bibro
Jakub Bibro

Reputation: 1640

Did you try putting @EnableAspectJAutoProxy(proxyTargetClass=true) on your configuration class?

Upvotes: 3

Related Questions