I am using AOP for the first time. I have written the below AOP code which works fine when i use it to intercept a particular method.
Can somebody guide me - how I can set it up to intercept all methods in a certain package (com.test.model)?
Basically how to setup appcontext.xml.
Also, do i need to do something like below to call before calling each method?
AopClass aoptest = (AopClass) _applicationContext.getBean("AopClass");
aoptest.addCustomerAround("dummy");
Can somebody help?
Please let me if some more explanation is needed.
Below is my code:
Interface:
package com.test.model;
import org.springframework.beans.factory.annotation.Autowired;
public interface AopInterface {
@Autowired
void addCustomerAround(String name);
}
Class:
package com.test.model;
import com.test.model.AopInterface;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class AopClass implements AopInterface {
public void addCustomerAround(String name){
System.out.println("addCustomerAround() is running, args : " + name);
}
}
AOP:
package com.test.model;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TestAdvice{
@Around("execution(* com.test.model.AopInterface.addCustomerAround(..))")
public void testAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("testAdvice() is running!");
}
}
appcontext:
<!-- Aspect -->
<aop:aspectj-autoproxy />
<bean id="AopClass" class="com.test.model.AopClass" />
<bean id="TestAdvice" class="com.test.model.TestAdvice" />
JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.
AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)
Join point: A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution. Join point information is available in advice bodies by declaring a parameter of type org. aspectj.
Executing method on the target class Thus, Spring AOP injects a proxy instead of an actual instance of the target class. When we start the Spring or Spring Boot application, we see Spring executes the advice before the actual method.
Just put:
@Around("execution(* com.test.model..*.*(..))")
The format of an execution expression is:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
where only ret-type-pattern
, name-pattern
and param-pattern
are required, so at least we need an expression like:
execution(ret-type-pattern name-pattern(param-pattern))
ret-type-pattern
matches the return type, *
for anyname-pattern
matches the method name, you can use *
as wildcard and ..
to indicate sub-packageparam-pattern
matches the method parameters, (..)
for any number of parametersYou can find more information here: 10. Aspect Oriented Programming with Spring, there are some useful examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With