First, what is AOP.
AOP (Aspect Orient programming), which is tangent-oriented programming . To be able to understand this, object-oriented programming (OOP) is to consider the program structure from a static perspective, and aspect-oriented programming (AOP) is to consider the program execution from a dynamic perspective .
Second, the role of AOP.
AOP is often used to deal with the crosscutting nature of systematic services , such as things management, security checks, caches, object pooling management , and so on, which has become a very common solution.
Third, the implementation of the principle of AOP.
AOP is actually implemented by the proxy class of the target class . an AOP proxy is actually an object that is dynamically generated by the AOP framework, which can be used as a target object . AOP agent includes all the methods of the target object, but the methods in the AOP proxy differ from the methods of the target object, and the AOP method adds enhanced processing to the particular pointcut and recalls the method of the target object .
Iv. support for AOP in Spring
The AOP agent in spring is generated and managed by the spring IOC container, and its dependencies are managed by the IOC container . Therefore, the AOP proxy can use the other Bean instances in the container directly as a target, and such a relationship can be provided by the dependency injection of the IoC container. Spring uses the Java dynamic proxy to create an AOP proxy by default , so that you can create proxies for whatever interface instances. When the proxy class is not a proxy interface, Spring will voluntarily switch to using the CGLIB proxy, or it can force the use of CGLIB.
AOP programming is actually a very easy thing to do. Throughout AOP programming, there are only three parts of the program ape that need to be involved:
- Define common business components.
- Define Pointcuts, where a pointcut can cross-cut multiple business components.
- Define enhanced processing, which is the processing action that the AOP framework weaves into ordinary business components.
So the key to AOP programming is defining pointcuts and defining enhanced processing. Once the appropriate pointcuts and enhancements have been defined, the AOP framework will proactively generate an AOP proxy, that is, the method of the Proxy object = The method of enhanced processing + the Proxied object .
V. Implementation of AOP in Spring.
Spring has the following two options for defining pointcuts and enhancing processing.
- Annotation-based "0 configuration" approach: Use @aspect, @Pointcut, and other Annotation to annotate pointcuts and enhance processing.
- Based on how XML configuration files are managed: Use the Spring configuration file to define pointcuts and enhancement points.
1, based on the Annotation "0 configuration" mode. (1), first enable Spring support for @AspectJ facets configuration.
<?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-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/ http Www.springframework.org/schema/beans/spring-aop-3.0.xsd "> <!--start support for @aspectj annotations-- <AOP: Aspectj-autoproxy/></beans>
Assuming that you do not intend to use spring's XML Schema configuration, you should add the following fragment in the spring configuration file to enable @aspectj support.
<!--enable @aspectj support--><bean class= " Org.springframeword.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator "/>
(2), define slice Bean.
When @aspectj support is started, just configure a bean with @aspect gaze in the spring container, and Spring will proactively identify the bean and treat it as a tangent.
Use @aspect to define a slice class @aspectpublic class Logaspect { //define other contents of the class ... }
(3), define before enhancement processing.
Define a facet @aspectpublic class Beforeadvicetest {//Match all the methods of all classes under the Com.wicresoft.app.service.impl package as pointcuts @before ("execution (* com.wicresoft.app.service.impl.*.* (..)) ") public void Authorith () {System.out.println ("Impersonation for permission check. ");}}
When using @before Annotation above, the pointcut expression is specified directly, specifying that all the methods of the class that match the Com.wicresoft.app.service.impl package run as a pointcut.
The rules about this expression are for example with.
(4), define afterreturning enhancement processing.
Define a facet @aspectpublic class Afterreturningadvicetest {//Match Com.wicresoft.app.service.impl All the methods under the package as Pointcuts @afterreturning (returning= "rvt", pointcut= "Execution (* com.wicresoft.app.service.impl.*.* (..))") public void log (object rvt) {System.out.println ("simulated target method return value:" + rvt); SYSTEM.OUT.PRINTLN ("Analog logging Function ...");}}
(5), define afterthrowing enhancement processing.
Define a facet @aspectpublic class Afterthrowingadvicetest {//Match Com.wicresoft.app.service.impl All the methods under the package are used as Pointcuts @afterthrowing (throwing= "Ex", pointcut= "Execution (* com.wicresoft.app.service.impl.*.* (..))") public void Dorecoveractions (Throwable ex) {System.out.println ("exception thrown in the target method:" + ex); SYSTEM.OUT.PRINTLN ("enhanced processing after impersonation throws an exception ...");}}
(6), define after enhancement processing.
After-enhancement processing is somewhat similar to afterreturning-enhanced processing, but there are differences:
- Afterreturning enhanced processing is only woven after the target method has been successfully completed.
- After-enhanced processing, regardless of how the target method ends (the save is successful and the exception is aborted), it will be woven into.
Define a facet @aspectpublic class Afteradvicetest {//Match all the methods of all classes under the Com.wicresoft.app.service.impl package as pointcuts @after ("Execution (* Com.wicresoft.app.service.impl.*.* (..)) ") public void Release () {System.out.println ("Frees resources after simulation method ends ...");}}
(7), Around enhanced processing
Around enhanced processing is approximately equal to the sum of before enhancement processing and afterreturning enhancement processing. It can change the parameters of the running target method, or it can change the return value after the target method.
Define a facet @aspectpublic class Aroundadvicetest {//Match all the methods of all classes under the Com.wicresoft.app.service.impl package as pointcuts @around ("execution (* com.wicresoft.app.service.impl.*.* (..)) ") Public Object Processtx (Proceedingjoinpoint JP) throws java.lang.Throwable {System.out.println ("Before running the target method, simulate start things ...") ;//Run the target method and save the return value after the target method is run object rvt = jp.proceed (new string[]{"changed"}); SYSTEM.OUT.PRINTLN ("Simulate end things before running the target method ..."); return rvt + "What's new";}}
(8), access to the target method of the number of references.
The simplest way to access the target method is to define an enhanced processing method that defines the first parameter as the Joinpoint type, and when the enhanced processing method is called, the Joinpoint parameter represents the join point for weaving the enhanced processing. Joinpoint included for example the following several regular usages.
- Object[] Getargs (): Returns the number of parameters when the target method is run.
- Signature getsignature (): Returns information about the method being enhanced.
- Object Gettarget (): Returns the target object that was woven into the enhanced processing.
- Object Getthis (): Returns the proxy object generated by the AOP framework for the target object.
tip : When using Around processing, we need to define the first parameter as the Proceedingjoinpoint type, which is a subclass of the Joinpoint type .
(9), define the entry point.
The so-called pointcut, in fact, is a name for a pointcut expression, thereby agreeing to reuse the name in multiple enhancement processes.
The Spring pointcut definition consists of two parts:
- A pointcut expression.
- A method signature that includes a name and a random number of parameters.
Specify Pointcut expression @pointcut ("Execution * transfer (..)" when using @pointcut Annotation) Using a return value of void, the method body is empty to name the pointcut private void Anyoldtransfer () {}//uses the pointcut defined above @afterreturning (pointcut= "Anyoldtransfer () ", returning=" ReVal ") public void Writelog (String msg, Object reVal) {...}
2, based on the XML configuration file management mode.
- Do not configure Pointcuts
<?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" xs i:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/sprin G-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/beans/spring-a Op-3.0.xsd "> <aop:config> <!--convert Fouradvicebean to slice Bean, the new name of the Slice Bean is: fouradviceaspect, specify the slice The priority is 2--<aop:aspect id= "Fouradviceaspect" ref= "Fouradvicebean" order= "2" > <!--define after-enhanced processing, straight Then specify the pointcut expression to use the Release () method in the slice Bean as an enhanced processing method--<aop:after pointcut= "Execution (* com.wicresoft.app.service.imp L.*.* (..)) " method= "Release"/> <!--define a before enhancement, specify pointcut expressions directly, and take the Authority () method in the slice Bean as an enhanced treatment--&L T;aop:before Pointcut= "Execution (* com.wicresoft.app.service.impl.*.* (..))" method= "authority"/> <!--define a Afterreturni ng enhancement processing, specifying pointcut expressions directly, using the log () method in the tangent Bean as an enhanced processing method--<aop:after-returning pointcut= "Execution (* com.wicresoft.a Pp.service.impl.*.* (..)) " method= "Log"/> <!--define a around enhancement, specify pointcut expressions directly, and take the Processtx () method in the slice Bean as an enhanced treatment method--<ao P:around pointcut= "Execution (* com.wicresoft.app.service.impl.*.* (..))" method= "Processtx"/> </aop:a Spect> </aop:config> <!--omitting the configuration of individual beans--<!--...--> </bean S>
<?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" xs i:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/sprin G-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/beans/spring-a Op-3.0.xsd "> <aop:config> <!--define a pointcut, mypointcut, directly know its corresponding pointcut expression--and <aop:pointcut Id= "Mypointcut" expression= "Execution (* com.wicresoft.app.service.impl.*.* (..))" method= "release"/> <aop:asp ECT id= "Afterthrowingadviceaspect" ref= "Afterthrowingadvicebean" order= "1" > <!--used above for pointcut definition enhanced processing-- <!--defines a afterthrowing enhancement process that specifies that the pointcut takes the Dorecovertyactions () method in the slice Bean as an enhanced processing method--<aop:after-throwing POI Ntcut-ref= "Mypointcut" method= "Dorecovertyactions" throwing= "ex"/> </aop:aspect> </aop:config> <!--omit each Configuration of Beans-<!--...--> </beans>
References:
"Lightweight Java EE Enterprise Application (third edition)" Li Gang
Spring3.0 AOP Specific explanations