java - In Spring Boot Application, annotation based pointcut is not executed -
hi have created spring boot application , trying apply aspect using spring aop. code 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 creating , 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 application.properties
contains following:
spring.aop.proxy-target-class=true
with above setting when bebug application using:
spring-boot:run "-drun.jvmarguments=-xdebug -xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
the aspect not executed if change pointcut @around("@annotation(org.my.pckg.annotation.timer)")
@around("execution( * org.my.pckg.*.*(..))")
works perfectly!
please find out what's missing in defining custom annotation..
change pointcut from:
@pointcut("execution(@org.my.pckg.annotation.timer)")
to:
@pointcut("@annotation(org.my.pckg.annotation.timer)")
read spring documentation on declaring pointcut.
Comments
Post a Comment