First, the introduction of AOP
The AOP full name is: aspect-oriented programming, it is the thought core facing the tangent number,
AOP and OOP are object-oriented programming languages that do not conflict, they are two mutually reinforcing design pattern types
AOP technology compensates for the lack of object-oriented programming, and Spring AOP is a technique to implement AOP, which is the core of a srping or sub-function in the spring framework.
Spring containers are not dependent on AOP
This means that programmers can choose whether or not to use AOP technology, AOP provides a powerful middleware solution, which is more sophisticated using the spring IOC container
Ii. Some terms 2.1, terminology
- Cross-cutting concern: Services at the system level are interspersed into the process of business logic
- Aspect: When needed, place it on top of the application and detach it from the application when it is not needed
- Advcie (notice): Is aspect specific implementation, advice including cross-cutting conerns behavior or the services provided
- Join point: Aspect the opportunity to join the business process when the application executes
- Pointcut (pointcut): Specifies that a aspect is interspersed to the application when those Joinpoint
- Target object: An object or target object to which advice is applied
- Instruction (introduced): For a class that has been written, compiled, and dynamically added at execution time without modifying or adding any code
- Weave (Weaving): The process applied to an object
2.2. Spring support for AOP
Plain Java language to write
Define Pointcutes can use configuration files
Jointpoints for attribute members are not supported. (Spring design thought that jointpoints that support attribute members would break the encapsulation of objects)
Third, spring create Advice (notice) 3.1, before Advice
Called before the method of the target object is executed
By implementing the Methodbeforeadvice interface to achieve
Methodbeforeadvice inherits from Beforeadvice, and Beforeadvice inherits the advice interface, and the Before method is called to execute before the method specified by target object target is executed. The before return value of void does not return a return value, and the method of executing the target object is not started until the before method finishes executing.
3.2. Example
Package com.pb;/** * Interface * @author Administrator * */public interface Ihello {public void SayHello (String str); }
Package com.pb;/** * Interface Implementation class * @author Administrator * */public class Hello implements Ihello { @Override public Voi D SayHello (String str) { System.out.println ("Hello " +str);} }
proxy class
Package Com.pb;import Java.lang.reflect.method;import org.springframework.aop.methodbeforeadvice;/** * Call before method execution * @ Author Administrator * Implements the Methodbeforeadvcie interface */public class Sayhellobeforeadvice implements Methodbeforeadvice { @Override public Void before (Method arg0, object[] arg1, Object arg2) throws Throwable { System.out.println ("= = =" Call ====== before the method executes);} }
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:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd "><!--set up the target object instance--><bean id=" Hello "class=" Com.pb.Hello "/><!--Set Advice Instance-- ><bean id= "Saybeforeadvice" class= "Com.pb.SayHelloBeforeAdvice"/><!--Establish proxy object--><bean id= " Hellproxy "class=" Org.springframework.aop.framework.ProxyFactoryBean "><!--set Proxy interface--><property Name=" Proxyinterfaces ><value>com.pb.IHello</value></property><!--Set the target object instance--><property Name= "target" ><ref local= "Hello"/></property><!--set Advice instance--><property name= " Interceptornames "><list><value>saybeforeadvice</value></list></property></ Bean></beans>
Test class
Package Com.pb;import Org.springframework.context.applicationcontext;import org.springframework.context.support.classpathxmlapplicationcontext;/** * Test class * @author Administrator * */public class Test {public static void Main (string[] args) { applicationcontext context=new classpathxmlapplicationcontext ( "Applicationcontext.xml"); Ihello ihello= (Ihello) Context.getbean ("Hellproxy"); Ihello.sayhello ("AOP");} }
Results:
= = Call ======hello AOP before the method executes
3.3. After Advice
Called after the target object method is executed
By implementing the Afterreturningadvice interface to achieve
Add on the above code
Package Com.pb;import Java.lang.reflect.method;import org.springframework.aop.afterreturningadvice;/* * Called after the method has finished executing * Implement Afterretruningadvice Interface */public class Sayafteradvice implements Afterreturningadvice { @Override public void Afterreturning (object arg0, Method arg1, object[] arg2, object Arg3) throws Throwable { System.out.pr INTLN ("========= calls ======= after the method has finished executing");} }
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:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd "><!--set up the target object instance--><bean id=" Hello "class=" Com.pb.Hello "/><!-- Set Beforadvice instance--><bean id= "Saybeforeadvice" class= "Com.pb.SayHelloBeforeAdvice"/><!-- Set Afteradvice instance--><bean id= "Sayafteradvice" class= "Com.pb.SayAfterAdvice"/><!--establish proxy object--><bean Id= "Hellproxy" class= "Org.springframework.aop.framework.ProxyFactoryBean" ><!--set Proxy interface--><property Name= "Proxyinterfaces" ><value>com.pb.IHello</value></property><!--Setting the target object instance-->< Property name= "target" ><ref local= "Hello"/></property><!--set Advice instance--><property name= " Interceptornames "≫<list><value>saybeforeadvice</value><value>sayafteradvice</value></list ></property></bean></beans>
The test class does not change, the result:
= = = Call ======hello before the method executes aop========= call ======= after the method finishes executing
3.4, Around Advice
Perform the appropriate action between and after the execution of the method
To implement an interface Methodinterceptor interface
Package Com.pb;import Org.aopalliance.intercept.methodinterceptor;import Org.aopalliance.intercept.methodinvocation;public class Sayaroundadvice implements Methodinterceptor { @ Override public Object Invoke (Methodinvocation arg0) throws Throwable { System.out.println ("========= Do something before the method is executed "); Object result=arg0.proceed (); System.out.println ("Do something ========= after the method is executed"); return result; } }
Configuration file
<?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:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd "><!--set up the target object instance--><bean id=" Hello "class=" Com.pb.Hello "/><!-- Set Beforadvice instance--><bean id= "Saybeforeadvice" class= "Com.pb.SayHelloBeforeAdvice"/><!-- Set Afteradvice instance--><bean id= "Sayafteradvice" class= "Com.pb.SayAfterAdvice"/><!--set Aroundadvice instance-- ><bean id= "Sayroundadvice" class= "Com.pb.SayAroundAdvice"/><!--Establish proxy object--><bean id= "Hellproxy" class= "Org.springframework.aop.framework.ProxyFactoryBean" ><!--set Proxy interface--><property Name= " Proxyinterfaces ><value>com.pb.IHello</value></property><!--Set the target object instance--><property Name= "Target" ><ref local= "Hello"/></property><!--setting advice instance--><property name= "Interceptornames" ><list> <!--<value>sayBeforeAdvice</value><value>sayAfterAdvice</value>--><value> Sayroundadvice</value></list></property></bean></beans>
3.5, Throwsadvice
When an exception occurs, notify a service object to do the processing
Implementing the Throwsadvice Interface
Package Com.pb;import Java.lang.reflect.method;import Org.springframework.aop.throwsadvice;public class Saythowsadvice implements Throwsadvice {public void afterthrowing (Method method,object[] objs,object target, Throwable ta) { System.out.println ("Exception occurred:" +ta+ "throws an exception:" +method);} }
Iv. XML-based Schema
Simplifying Code implementation
Easy to maintain with the corresponding program
All elements are defined in <aop:config>
V. Based on annotation
Annotate Java generic classes as annotations
Spring (v) AOP Brief