1. Download the jar
//core spring
compile "org.springframework:spring-context:$springVersion"
compile "org.springframework:spring-core:$springVersion"
compile "org.springframework:spring-webmvc:$springVersion"
compile "org.springframework:spring-aop:$springVersion"
compile "org.springframework:spring-expression:$springVersion"
compile "org.springframework:spring-beans:$springVersion"
compile "org.springframework:spring-aspects:$springVersion"
compile group: 'org.aopalliance', name: 'com.springsource.org.aopalliance', version: '1.0.0'
compile group: 'org.aspectj', name: 'com.springsource.org.aspectj.weaver', version: '1.6.8.RELEASE'
1. Define the interface
package com.boce.aop;
public interface AopTest {
public int add(int i, int j);
public int sub(int i, int j);
public int mul (int i, int j);
public int div(int i, int j);
}
2. Interface implementation:
package com.boce.aop;
import org.springframework.stereotype.Component;
@Component
public class AopTestImp implements AopTest{
public int add(int i, int j) {
int result = i + j;
return result;
}
public int sub(int i, int j) {
int result = i - j;
return result;
}
public int mul (int i, int j) {
int result = i * j;
return result;
}
public int div(int i, int j) {
int result = i / j;
return result;
}
}
3.aop interception:
package com.boce.aop;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Order(1)
@Aspect
@Component
public class AopLogAspect {
/**
* Declare a pointcut expression, and generally no other code is added to this method.
* Use @Pointcut to declare pointcut expressions.
* The advice that follows directly uses the method name to refer to the current pointcut expression.
*/
@Pointcut("execution(public int com.boce.aop.AopTestImp.*(..))")
public void declareJoinPointExpression() {}
/**
* Pre-advice, executed before the target method starts.
*@Before("execution(public int com.spring.aop.impl.ArithmeticCalculator.add(int, int))") can be written like this to specify a specific method.
* @param joinpoint
*/
@Before("declareJoinPointExpression()")
// Use the pointcut expression here. The latter can be changed to pointcut expressions. If the pointcut expression is in another package, just add the package name and class name in front.
public void beforeMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List<Object>args = Arrays.asList(joinpoint.getArgs());
System.out.println("前置通知:The method "+ methodName +" begins with " + args);
}
/**
* Post-advice, start execution after the target method executes, regardless of whether the target method throws an exception.
* The result of the target method execution cannot be accessed in the post-notification.
* @param joinpoint
*/
@After("execution(public int com.boce.aop.AopTestImp.*(int, int))")
public void afterMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
//List<Object>args = Arrays.asList(joinpoint.getArgs()); Parameters can be obtained in the post notification method
System.out.println("后置通知:The method "+ methodName +" ends ");
}
/**
* Return notification, which is executed after the method ends normally.
* Can access the return value of the method.
* @param joinpoint
* @param result The return value of the target method
*/
@AfterReturning(value="execution(public int com.boce.aop.AopTestImp.*(..))", returning="result")
public void afterReturnning(JoinPoint joinpoint, Object result) {
String methodName = joinpoint.getSignature().getName();
System.out.println("返回通知:The method "+ methodName +" ends with " + result);
}
/**
* Exception notification. The target method is executed when an exception occurs, the exception object can be accessed, and it can be specified to execute only when a specific exception occurs.
*If the parameter is written as NullPointerException, it will only be executed when a null pointer exception occurs.
* @param joinpoint
* @stop me
*/
@AfterThrowing(value="execution(public int com.boce.aop.AopTestImp.*(..))", throwing="e")
public void afterThrowing(JoinPoint joinpoint, Exception e) {
String methodName = joinpoint.getSignature().getName();
System.out.println("异常通知:The method "+ methodName +" occurs exception " + e);
}
/**
* Surround notification is similar to the whole process of dynamic proxy, the parameter of type ProceedingJoinPoint can decide whether to execute the target method.
* @param point Surround notifications need to carry parameters of type ProceedingJoinPoint.
* @return The return value of the target method. There must be a return value.
*/
/*
@Around("execution(public int com.boce.aop.AopTestImp.*(..))")
public Object aroundMethod(ProceedingJoinPoint point) {
Object result = null;
String methodName = point.getSignature().getName();
try {
//pre-notification
System.out.println("The method "+ methodName +" begins with " + Arrays.asList(point.getArgs()));
//Execute the target method
result = point.proceed();
//Translation notification
System.out.println("The method "+ methodName +" ends with " + result);
} catch (Throwable e) {
//exception notification
System.out.println("The method "+ methodName +" occurs exception " + e);
throw new RuntimeException(e);
}
// post notification
System.out.println("The method "+ methodName +" ends");
return result;
}
*/
}
3. Configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<context:component-scan base-package="com.boce.aop">
</context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
4. Test class
package com.boce.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.boce.aop.AopTest;
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = { "classpath:aopContext-service.xml" })
public class AopTestMain {
public static void main(String[] args) {
// Create spring IOC container
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("aopContext-service.xml");
// Get the bean instance from the IOC container
AopTest aopTest = applicationContext.getBean(AopTest.class);
int result = aopTest.add(4, 6);
System.out.println(result + "");
result = aopTest.sub(4, 6);
System.out.println(result);
System.out.println(result);
result = aopTest.mul (4, 6);
System.out.println(result);
System.out.println(result);
result = aopTest.div(4, 0);
System.out.println(result);
}
}
Note: When aop interception is performed on the Controller layer, the configuration file must be configured in the spring-mvc file, otherwise the AOP will be invalid
Results of the: