1. AOP related concepts and principles
1. Development process
Suppose that for an existing class Student, its function is extended:
- Traditional way : modify directly in the source code; troublesome and error-prone, difficult to maintain
- Vertical extraction mechanism extension : if you write an extension class, write an extension method in this class, let Student base this class, and call the extension method of this class; the coupling degree is still too high, when the parent class changes, you still need to adjust the values in Student Call form (such as method name change, parameter change, etc.)
- Horizontal extraction mechanism extension: and Aspect-Oriented Thought (AOP), which is realized by the principle of dynamic proxy.
- Scenario 1: Student implements an interface, creates a proxy object that implements the interface at the same level as Student to extend, and JDK dynamic proxy
- Case 2: Student does not implement the interface, then create a proxy object of the Student subclass, call the method in super (parent class) in the subclass, and complete the enhancement, that is, cglib dynamic proxy
2. Related concepts
- JoinPoint: Methods that can be enhanced
- PointCut: The method that actually enhances
Advice/Advice: The actual enhancement
- Pre-Notification: Enhanced Before Methods
- Post-Notification: Enhanced After Method
- Exception notification: Enhanced after an exception occurs in a method
- Final notification: Notify after post
- Surrounding notification: The enhanced method needs to add (ProceedingJoinPoint pjp) formal parameters, call pjp.proceed(); that is, call the enhanced method
Aspect: The process of applying enhancements to pointcuts
2. AOP operation with Spring and Aspectj (xml configuration method)
1、引入aop的jar包 aopalliance、aspectjweaver、spring-aop、spring-aspects
2. Add constraints
<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">
</Beans>
3. Create the Student class and the class StudentAdd of the entry point
public class Student {
public void Show() {
System.out.println("原始:我是一个小学生...");
}
}
public class StudentAdd {
//前置增强
public void ShowBefore() {
System.out.println("前置:我的名字叫小明...");
}
//后置增强
public void ShowAfter() {
System.out.println("后置:我上二年级....");
}
//最终增强
public void ShowFinal() {
System.out.println("最终:我要好好学习...");
}
//环绕增强
public void ShowAro(ProceedingJoinPoint pj) throws Throwable {
System.out.println("前环绕:============");
pj.proceed(); //执行方法
System.out.println("后环绕:************");
}
}
4. Aspectj configures AOP operations
<!-- 扫描包下的所有注解 -->
<context:component-scan base-package="com.aoptest"></context:component-scan>
<!-- 配置对象的创建 -->
<bean id="student" class="com.aoptest.test1.Student"></bean>
<bean id="add" class="com.aoptest.test1.StudentAdd"></bean>
<!-- aop配置 -->
<aop:config>
<!-- 配置切入点(那些方法要增强) -->
<aop:pointcut expression="execution(* com.aoptest.test1.Student.Show(..))" id="show"/>
<!-- 切面配置(将增强应用到上面的方法) -->
<aop:aspect ref="add">
<aop:before method="ShowBefore" pointcut-ref="show"/>
<aop:after method="ShowAfter" pointcut-ref="show"/>
<aop:after-returning method="ShowFinal" pointcut-ref="show"/>
<aop:around method="ShowAro" pointcut-ref="show"/>
</aop:aspect>
</aop:config>