Simple introduction to AOP XML configuration
- Import AOP related coordinates
- Create target interface and target class
- Create facet class (with enhancement method inside)
- Give the object creation right of the aspect class of the target class to Spring
- In ApplicationContext Configuring weaving relationships in XML
- Test code
<dependecy> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependecy> <dependecy> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.0</version> </dependecy>
//Target interface public interface TargetInterface { void save(); }
//Target class public class Target implements TargetInterface{ public void save(){ System.out.println("save running..."); } }
//Section class public class MyAspect{ public void before(){ System.out.println("-------------Pre enhancement-------------"); } }
<!--applicationContext.xml--> <?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: 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--Target object--> <bean id="target" class="com.jotian.aop.Target"></bean> <!--Tangent object--> <bean id="myAspect" class="com.jotian.aop.MyAspect"></bean> <!--Configure weaving, tell Spring Framework, which methods need to be enhanced (pre, post)--> <aop:config> <!--Declaration section--> <aop:aspect ref="myAspect"> <!--Pointcut notification--> <aop:before method="before" pointcut="execution(public void com.jotian.aop.Target.save()) "></aop:before> </aop:aspect> </aop:config> </beans>
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class AopTest{ @Autowired private TargetInterface target; @Test public void test1(){ target.save(); } }
Detailed explanation of XML configuration AOP
-
Writing of tangent point expression
Expression syntax:
execution([Modifier ]Return value type package name.Class name.Method name(parameter))
- The access modifier can be omitted
- The returned value type, package name, class name and method name can be represented by an asterisk *
- A point between the package name and the class name represents the class under the current package. Two points Represents the classes under the current package and its sub packages
- The parameter list indicates that two points can be used Represents any number and any type of parameter list
For example:
execution(public void com.jotian.aop.Target.method()) execution(void com.jotian.aop.Target.*(..)) execution(* com.jotian.aop..*.*(..)) execution(* com.jotian.aop.*.*(..)) execution(* *..*.*(..))
-
Type of notification
Configuration method of notification:
< AOP: notification type method = "method name in facet class" pointcut = "pointcut expression" > < / AOP: notification type >
name label explain Before advice < aop:before > Used to configure pre notification. Specifies that the enhanced method is executed before the pointcut method Post notification < aop:after-returning> Used to configure post notifications. Specifies that the enhanced method is executed after the pointcut method Around Advice < aop:around> Used to configure surround notifications. Specifies that the enhanced method is executed before and after the pointcut method Exception throw notification < aop:throwing> Used to configure exception throw notifications. Specifies that the enhanced method is executed when an exception occurs Final notice < aop:after> Used to configure final notifications. The enhanced mode will be executed regardless of whether there are exceptions -
Extraction of tangent expression
When multiple enhanced pointcut expressions are the same, the pointcut expression can be extracted. In the enhancement, the pointcut ref attribute is used instead of the pointcut attribute to reference the extracted pointcut expression.
<aop:config> <!--quote muAspect of Bean Is a tangent object--> <aop:aspect ref="myAspect"> <!--Extract tangent expression--> <aop:pointcut id="myPointcut" expression="execution(* com.jotian.aop.*.*(..))"></aop:pointcut> <!--Tangent plane: tangent point+notice--> <aop:before method="before" pointcut-ref="myPointcut"></aop:before> </aop:aspect> </aop:config>
Key points of knowledge
-
aop weaving configuration
<aop:config> <aop:aspect ref="Front class"> <aop:before method="Notification method name" pointcut="Tangent expression"></aop:before> </aop:aspect> </aop:config>
-
Notification types: pre notification, post notification, surround notification, exception throw notification, and final notification
-
How to write tangent point expression:
execution([Modifier ]Return value type package name.Class name.Method name(parameter))