AOP concepts
1 aop: Aspect oriented programming, extensions do not modify the source code implementation.
2 aop takes the horizontal extraction mechanism instead of the traditional vertical inheritance system duplicate code.
3 aop underlying implementation using dynamic proxy
(1) In the first case, if there is an interface case, use the dynamic proxy to create an interface to implement the class proxy object.
(2) In the second case, where there is an interface case, a subclass proxy object of a class is created using a dynamic proxy.
AOP Principle
(1) Overview of AOP
(2) underlying principles of aop
(3) Terminology related to aop operation
AOP operation terminology
Joinpoint: A connection point is a point that has been intercepted.These points refer to methods in spring because spring only supports methods
Connection point of type.
Pointcut: The entry point refers to the definition of those Joinpoint s that we want to intercept.
Advice: Notification means that what you do after intercepting a Joinpoint is notification.Notifications are divided into pre-notifications,
Post notification, exception notification, final notification, surround notification (functionality to be accomplished by facets)
Aspect: is the combination of entry point and notification (introduction).
Introduction: Introduction is a special notification that an Introduction can be run without modifying the class code
Period dynamically adds some methods or fields to the class.
Target: The target object of the proxy (the class to be enhanced)
Weaving: is the process of applying enhancements to the target and applying advice s to the target.
Proxy: A result proxy class is produced when a class is enhanced by AOP weaving.
Sping's aop operation
1 aop operation in spring, implemented with aspectj
(1) aspectj is not part of spring and is used with spring for aop operations.
(2) AspectJ support has been added since Spring 2.0.
2 There are two ways to implement aop using aspectj
(1) xml configuration based on aspectj
(2) Notes based on aspectj
Aop operation preparation
1 In addition to importing basic jar packages, you also need to import aop-related jar packages.
2 Create a spring core configuration file and import aop constraints.
<?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"> <!-- bean definitions here --> </beans>
Configuring entry points using expressions
1. Start Point: Ways to Actual Enhancement
2 Common expressions
Execution (<access modifier>?<return type><method name>(<parameter>)<exception>)
(1) execution(*com.cn.aop.Book.add(.))* represents any modifier.. represents any parameter
(2)ececution(*com.cn.aop.Book.*(..))
(3) execution(**.*(..))
(4) Match execution(*save*(..)) of all save methods starting with save
Example:
bean3.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"> <!-- bean definitions here --> <!-- 1 Configuration object --> <bean id="book" class="com.cn.aop.Book"></bean> <bean id="myBook" class="com.cn.aop.MyBook"></bean> <!-- 2 To configure aop Operation --> <aop:config> <!-- 2.1 Configure entry points //Enhanced Class--> <aop:pointcut id="pointcut1" expression="execution(* com.cn.aop.Book.*(..))" /> <!-- 2.2 Configure Faces to Use Enhancements on Methods --> <aop:aspect ref="myBook"> <!-- Configuration Enhancement Class method: Which method to use as a precedent in an enhanced class pointcut-ref hold before1 What entry point to use for enhancement --> <aop:before method="before1" pointcut-ref="pointcut1"/> </aop:aspect> </aop:config> </beans>
Book.java
package com.cn.aop; public class Book { public void add(){ System.out.println("add........"); } }
MyBook.java
package com.cn.aop; public class MyBook { public void before1(){ System.out.println("Pre-Enhancement"); } }
TestAnno.java
package com.cn.aop; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAnno { @Test public void testService(){ ApplicationContext context= new ClassPathXmlApplicationContext("bean3.xml"); Book book=(Book) context.getBean("book"); book.add(); } }
test result