Spring AOP pointcut that matches annotation on interface

If I understand you correct, you want a pointcut that finds all methods in classes that extends MyService and is annotated and with the preferred arguments. I propose that you replace: execution(public * com.mycompany.myserviceimpl.*(..)) with: execution(public * com.mycompany.myservice.MyService+.*(..)) The plus sign is used if you want a joinpoint to match the MyService class or a … Read more

Spring AOP works without @EnableAspectJAutoProxy?

The @SpringBootApplication annotation contains the @EnableAutoConfiguration annotation. This autoconfiguration is one of the attractions of Spring Boot and makes configuration simpler. The auto configuration uses @Conditional type annotations (like @ConditionalOnClass and @ConditionalOnProperty) to scan the classpath and look for key classes that trigger the loading of ‘modules’ like AOP. Here is an example AopAutoConfiguration.java import … Read more

AspectJ “around” and “proceed” with “before / after”

With this Test @Aspect public class TestAspect { private static boolean runAround = true; public static void main(String[] args) { new TestAspect().hello(); runAround = false; new TestAspect().hello(); } public void hello() { System.err.println(“in hello”); } @After(“execution(void aspects.TestAspect.hello())”) public void afterHello(JoinPoint joinPoint) { System.err.println(“after ” + joinPoint); } @Around(“execution(void aspects.TestAspect.hello())”) public void aroundHello(ProceedingJoinPoint joinPoint) throws Throwable … Read more

Lombok and AspectJ

Use ajc to process classes. <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.11</version> <configuration> <complianceLevel>8</complianceLevel> <source>8</source> <target>8</target> <showWeaveInfo>true</showWeaveInfo> <verbose>true</verbose> <Xlint>ignore</Xlint> <encoding>UTF-8</encoding> <!– IMPORTANT–> <excludes> <exclude>**/*.java</exclude> </excludes> <forceAjcCompile>true</forceAjcCompile> <sources/> <!– IMPORTANT–> <aspectLibraries> <aspectLibrary> <groupId>you.own.aspect.libary</groupId> <artifactId>your-library</artifactId> </aspectLibrary> </aspectLibraries> </configuration> <executions> <execution> <id>default-compile</id> <phase>process-classes</phase> <goals> <!– use this goal to weave all your main classes –> <goal>compile</goal> </goals> <configuration> <weaveDirectories> <weaveDirectory>${project.build.directory}/classes</weaveDirectory> </weaveDirectories> … Read more

How to use AOP with AspectJ for logging?

I have created a simple aspect to capture the execution of public methods. The core of this AspectJ code is the pointcut definition: pointcut publicMethodExecuted(): execution(public * *(..)); Here we are capturing all public methods with any return type, on any package and any class, with any number of parameters. The advice execution could be … Read more

@AspectJ pointcut for all methods inside package

How about one of these alternatives? A) General execution pointcut with package restrictions: execution(* *(..)) && ( within(com.abc.xyz..controller..*) || within(com.abc.xyz..service..*) || within(com.abc.xyz..dao..*) ) B) Package-restricted execution pointcuts: execution(* com.abc.xyz..controller..*(..)) || execution(* com.abc.xyz..service..*(..)) || execution(* com.abc.xyz..dao..*(..)) I prefer B, by the way, just because it is a bit shorter and easier to read. As you have … Read more

@Transactional in super classes not weaved when using load time weaving

Tomcat default classlLoader is WebappClassLoader, but you need `TomcatInstrumentableClassLoader. There are two solutions: Modify WebappLoader.class Change WebappLoader.java private String loaderClass = “org.apache.catalina.loader.WebappClassLoader”; Replace: private String loaderClass = “org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader”; Compile it and replaced the class file(catalina.jar), then it works. Here is required dependency jars: catalina.jar,tomcat-coyote.jar,tomcat-util.jar(/bin),tomcat-juli.jar Modify context.xml: <?xml version=”1.0″ encoding=”UTF-8″?> <Context> <Loader loaderClass=”org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader”/> </Context>

tech