Foreword
aop framework is part of the core of the spring
1. What is the aop
AOP (Aspect Oriented Programming), commonly referred to as Aspect Oriented Programming. It uses a technique called "transverse", the internal cross-sectional decapsulates the object, and those that affect the behavior of the public classes encapsulated into a plurality of reusable modules, and named it "Aspect", i.e., section . The so-called "cut", it simply is that nothing to do with business, but it is the responsibility of the business logic or common module called encapsulated, easy to duplicate code to reduce system and reduce the degree of coupling between modules, and facilitate future operability and maintainability.
What is the aspect, what is the common module, then our concept less directly through an example to see what AOP in the end yes.
Key terms of 2.aop
1.target: target class, to be the agent class. For example: UserService
2.Joinpoint (connection points): a so-called point of attachment are those methods may be intercepted. For example: all methods
3.PointCut entry point: the connection point has been enhanced. For example: addUser ()
4.advice notice / enhancement, enhancement code. For example: after, before
5. Weaving (weaving): refers to the application of reinforced advice to target audiences to the process of creating a new proxy object proxy.
6.proxy proxy class: the entry point notification +
7. Aspect (section): the entry point is a combination of the advice notification and pointcut
3.aop types of notifications
Advice in accordance with the notice Spring connection point position of the target class methods can be divided into five categories
- Before advice org.springframework.aop.MethodBeforeAdvice
- Before performing certain embodiment enhancement method, such as the above example before () method
- After returning advice org.springframework.aop.AfterReturningAdvice
- After execution of the target method to enhance embodiment, such as the above example after () method
- Around advice org.aopalliance.intercept.MethodInterceptor
- Before and after the target method to enhance the implementation of the Executive
- An exception is thrown notice org.springframework.aop.ThrowsAdvice
- After the implementation of enhanced method throws an exception
- Introducing notification org.springframework.aop.IntroductionInterceptor
Add some new methods and properties in the target class
4 illustrates
UserService class is a service layer, there are three methods
Now there is a demand, requiring both before and after the implementation of these three methods to open and close the transaction
In particular this may be understood from the following chart:
5. For this requirement, generally have several solutions
1. Static Proxy
2. Use jdk dynamic proxy (requirement must implement the interface)
3. Another mode of realization dynamic proxy Gglib, the need to implement the interface
In this respect these do not dwell
This chapter: aop also be achieved by the principle of dynamic proxies
Personal Stories
1. Create a maven project
2. Add relevant dependencies in pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cong</groupId> <artifactId>spring_aop</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> </dependencies> </project>
3. Create com.cong.pojo.User class in the java directory
1 package com.cong.pojo; 2 3 public class User { 4 private int id; 5 private String name; 6 7 public int getId() { 8 return id; 9 } 10 11 public void setId(int id) { 12 this.id = id; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 }
4. Create com.cong.service.UserService interface java directory
1 package com.cong.service; 2 3 import com.cong.pojo.User; 4 5 public interface UserService { 6 void addUser(User user); 7 void deleteUser(int id); 8 }
5. Create the implementation class UserServiceImpl in the service directory
1 package com.cong.service; 2 3 import com.cong.pojo.User; 4 5 public class UserServiceImpl implements UserService { 6 @Override 7 public void addUser(User user) { 8 System.out.println("add user:"+user.getName()); 9 } 10 11 @Override 12 public void deleteUser(int id) { 13 System.out.println("delete user: "+id); 14 } 15 }
6. Create com.cong.ui.MyTransaction class in the java directory
. 1 Package com.cong.ui; 2 . 3 public class MyTransaction { . 4 public void before () { . 5 System.out.println ( "Open Services ..." ); . 6 } . 7 public void After () { . 8 the System.out .println ( "Close transaction ..." ); 9 } 10 }
7. Create a context.xml file in the resources directory
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop.xsd"> 9 <bean id="user" class= "com.cong.pojo.User"> 10 <Property name = "ID" value = ". 1"> </ Property> . 11 <Property name = "name" value = "Cong"> </ Property> 12 is </ the bean> 13 is <the bean ID = "that userService" class = "com.cong.service.UserServiceImpl"> </ the bean> 14 <the bean ID = "myTransaction" class = "com.cong.ui.MyTransaction"> </ the bean> 15 <! - configuration AOP -> 16 <AOP: config> 17 <- - entry point expression, the so-called entry points that you need to enhance the way!> 18 <AOP: pointcut the above mentioned id = " myPointcut" expression="execution(* com.cong.service.UserServiceImpl.*(..))"></aop:pointcut> 19 <! - configuration section, the section is to enhance needed during the classes -> 20 is <AOP: Aspect REF = "myTransaction"> 21 is ! <- configuration type of notification, the notification method and the starting point and the method of establishing association -> 22 is ! <- "before" is MyTransaction method -> 23 is <AOP: before method = "before" the pointcut-REF = "myPointcut"> </ AOP: before> 24 <-! If no myPointcut configuration, the above statement can be written -> 25 <- <AOP:! before Method = "before" = the pointcut "Execution (com.cong.service.UserServiceImpl * * (..).)"> < / AOP: before> -> 26 is <AOP: After returning-Method = "After" the pointcut-REF = "myPointcut"> </ AOP: after-returning> 27 </aop:aspect> 28 </aop:config> 29 ! <- spring arranged in the XML-based AOP step 30 . 1 , the notification Bean also managed to spring 31 is 2 , the use of aop: config tag to indicate the beginning of AOP configuration 32 . 3 , using aop: aspect tag indicates that the configuration section 33 is ID properties: provides a unique identifier to section 34 is REF attribute: Specifies a notification type is the bean Id. 35 . 4 , in aop: corresponding tag type using the internal aspect tag to configure notification 36 we let printLog exemplary method of performing the method before the pointcut before: it is a pre-notification 37 [ aop: before: Pre-configured notification indicates 38 method attribute: Logger class which specifies the pre-notification is 39 the pointcut properties: pointcuts for expression, the expression refers to the meaning of which is a method of enhancing the business layer 40 41 is written expression pointcut : 42 Keywords: Execution (expression) 43 expression: 44 access modifier return value package names package names package name ... class name method name (parameter list)... 45 standard written expression: 46 is public void COM. itheima.service.impl.AccountServiceImpl.saveAccount () 47 access modifiers may be omitted 48 void com.itheima.service.impl.AccountServiceImpl.saveAccount () 49 return value can use wildcards, represent any return value 50 * com.itheima.service .impl.AccountServiceImpl.saveAccount () 51 is the package name can use wildcards, it indicates any package. But there are a few levels pack, you need to write a few * . 52 *. *. *. *. *.AccountServiceImpl.saveAccount ()) 53 is the package name may be used .. represent the current sub-packet and packet 54 is * * ..AccountServiceImpl.saveAccount () 55 class and method names * can be used to implement the wildcard 56 * * .. * . * () 57 parameter list: 58 can directly write data type: 59 base type name written directly int 60 . write reference type package name class name manner java.lang.String 61 is wildcards can be used to represent any type, but there must parameters 62 may be used .. indicating whether parameters can be, there may be any type parameter 63 allpass with wording: 64 . * .. * * *(..) 65 66 actual development pointcut expressions usually written: 67 cutting method in all business layer implementation class 68 * * * com.itheima.service.impl.. (..) 69 -> 70 </ beans>
8. Create TestAop class in test.java directory
1 import com.cong.pojo.User; 2 import com.cong.service.UserService; 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class TestAop { 7 public static void main(String[] args) { 8 ApplicationContext context = new ClassPathXmlApplicationContext("context.xml"); 9 UserService userService = (UserService) context.getBean("userService"); 10 User user = (User)context.getBean("user"); 11 userService.addUser(user); 12 userService.deleteUser(10); 13 } 14 }
9. Run results
10. Full catalog
Reference: https://www.cnblogs.com/ysocean/p/7476379.html#_label6