简体   繁体   中英

In Spring Boot Application, annotation based pointcut is not executed

Hi I have created spring boot application and trying to apply Aspect using spring AOP. The code is as below ...

The custom Timer annotation

package org.my.pckg.annotation;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Timer {
}

The TimerLoggingAspect Aspect

@Aspect
@Component
public class TimeLoggingAspect {

@Pointcut("annotation(@org.my.pckg.annotation.Timer)")
public void loggingPointCutDefinition(){}

    @Around("loggingPointCutDefinition()")
    public void userAdvice(ProceedingJoinPoint joinPoint) throws Throwable{
        createJsonString(joinPoint);
        joinPoint.proceed();
    }

    private String createJsonString(ProceedingJoinPoint joinPoint) {
    //logic for creating and printing json
        return "";
    }
}

The config class

package org.my.pckg.config;
@Configuration
@EnableAspectJAutoProxy  
@ComponentScan(basePackages =     {"org.my.pckg.utilities","org.my.pckg.annotation"})
public class AssetConfig {

    @Bean
    public TimeLoggingAspect timeLoggingAspect() {
        return new TimeLoggingAspect();
    }
}

The Example test controller

package org.my.pckg;
@SpringBootApplication
@PropertySources({
    @PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true)
})
@Configuration
@ComponentScan(basePackages = {"org.my.pckg.config","org.my.pckg.annotation"})
@RestController
@EnableAutoConfiguration
@EnableAspectJAutoProxy
public class Example {

@Timer
@RequestMapping("/")
String home() {
    return "Hello World!";
}

public static void main(String[] args) throws Exception {
    SpringApplication.run(Example.class, args);
}
}

And the application.properties contains the following:

spring.aop.proxy-target-class=true

With the above setting when I bebug the application using:

spring-boot:run "-Drun.jvmArguments=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

The Aspect is not executed but if I change the pointcut from @Around("@annotation(org.my.pckg.annotation.Timer)") to @Around("execution( * org.my.pckg.*.*(..))") It works perfectly!

Please help to find out what's missing in defining custom annotation..

Change your pointcut from:

@Pointcut("execution(@org.my.pckg.annotation.Timer)")

To:

@Pointcut("@annotation(org.my.pckg.annotation.Timer)")

Read the Spring Documentation on Declaring a Pointcut .

After many trial and error I found a solution for the above problem.

Create a file aop.xml and put it inside resource /META-INF/ with following content

<?xml version="1.0"?>
<aspectj>
<!-- place all aspects here -->
<aspects>
    <aspect name="<full qualified name of aspect class>"/>
</aspects>
<weaver options="-verbose -showWeaveInfo">
</weaver>

Thanks to http://foat.me Find more detailed solution here http://foat.me/articles/java-aspects-using-spring-aop-and-aspectj/#internal-method-calls

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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