1.Introduce aop related dependencies
<!-- spring-aop dependency --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>6.0.2</version> </dependency> <!-- spring aspects dependency--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>6.0.2</version> </dependency>
2. Create target resources
①Interface
public interface Calculator { int add(int i,int j); int sub(int i,int j); int mul(int i,int j); int div(int i,int j); }
②Implementation class
@Component public class CalculatorImpl implements Calculator { @Override public int add(int i, int j) { int result = i + j; System.out.println("method internal result = " + result); return result; } @Override public int sub(int i, int j) { int result = i - j; System.out.println("method internal result = " + result); return result; } @Override public int mul(int i, int j) { int result = i * j; System.out.println("method internal result = " + result); return result; } @Override public int div(int i, int j) { int result = i / j; System.out.println("method internal result = " + result); return result; } }
3. Create aspect classes
//Aspect class @Aspect//Aspect class @Component//ioc container management public class LogAspect { }
Configuration bean file
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- Turn on component scanning --> <context:component-scan base-package="com.yogurt.spring6.aop.annoaop"></context:component-scan> <!-- Turn on aspectj automatic proxy and generate a proxy for the target object --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
①Entry point
pointcut expression
Example:
Pre-notification
@Aspect//Aspect class @Component//ioc container management public class LogAspect { //Set entry point and notification type //Pointcut expression: execution (access modifier, enhanced method return type, full path of the class where the enhanced method is located. Method name (method parameter)) //Notification type: // Prefix @Before(value = "Pointcut expression configuration pointcut") @Before(value = "execution(public int com.yogurt.spring6.aop.annoaop.CalculatorImpl.*(..))") public void beforeMethod(){ System.out.println("Logger-->Pre-notification"); } }
Post notification
// Post @After() @After(value = "execution(* com.yogurt.spring6.aop.annoaop.CalculatorImpl.*(..))") public void afterMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName();//Get the name of the enhanced method System.out.println("Logger-->Post notification, method name: " + methodName); }
Return notification
// Return @AfterReturning() @AfterReturning(value = "execution(* com.yogurt.spring6.aop.annoaop.CalculatorImpl.*(..))",returning = "result") public void afterReturningMethod(JoinPoint joinPoint,Object result){ String methodName = joinPoint.getSignature().getName();//Get the name of the enhanced method System.out.println("Logger-->return notification, method name: " + methodName + ", return result: " + result); }
Exception notification
//Exception @AfterThrowing() obtains the target method exception information //An exception occurs in the target method, this notification is executed @AfterThrowing(value = "execution(* com.yogurt.spring6.aop.annoaop.CalculatorImpl.*(..))",throwing = "ex") public void afterThrowingMethod(JoinPoint joinPoint,Throwable ex){ String methodName = joinPoint.getSignature().getName();//Get the name of the enhanced method System.out.println("Logger-->Exception notification, method name: " + methodName + ", exception information: " + ex); }
At this time, the notification can only appear if the exception is set:
@Component public class CalculatorImpl implements Calculator { @Override public int add(int i, int j) { int result = i + j; System.out.println("method internal result = " + result); // //For testing purposes, simulate exceptions. // int a = 1/0; return result; } }
Surround notifications
// Surround @Around() @Around(value = "execution(* com.yogurt.spring6.aop.annoaop.CalculatorImpl.*(..))") public Object aroundMethod(ProceedingJoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName();//Get the name of the enhanced method Object[] args = joinPoint.getArgs(); String argString = Arrays.toString(args); Object result = null; try{ System.out.println("Surround notification == executed before target method"); //Call target method result = joinPoint.proceed(); System.out.println("Surround notification == executed after the target method returns the value"); }catch (Throwable throwable){ throwable.printStackTrace(); System.out.println("Surround notification == abnormal execution of target method"); }finally { System.out.println("Surround notification == target method execution completed"); } return result; }
②Notification type
Prefix @before()
Return @AfterReturning()
Exception @AfterThrowing()
Post @After()
Around @Around()
4. Test
public class TestAop { @Test public void testAdd(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); Calculator calculator = context.getBean(Calculator.class); calculator.add(2,3); } }
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java skill treeNotesBasic grammar 139165 people are learning the system