AOP (aspect-orientedprogramming) aspect-oriented programming, unlike OOP, is divided into aspects or concerns, rather than objects in OOP, using an AOP programming system.
introduction of AOP
In OOP object-oriented use, there will inevitably be code duplication, and the use of object-oriented programming, such duplication can not be avoided, such as user rights to judge, according to the corresponding permissions to execute the appropriate method; When you set the encoding format in the servlet, the same code appears many times, But also root business unrelated, it is easy to forget to write, the results run when the garbled pull appears. This duplication of code not only makes coding cumbersome, but it is not easy to maintain. AOP, in turn, collates the code and puts the code that solves a facet problem in a separate module and then weaves it into the program. terminology in AOP
Aspect: Cross-sectional function, abstract out of class, or interface, AOP programming is important to recognize the cross-sectional function.
( aspect, similar to character encoding function )
Advice: The specific implementation of cross-sectional function, according to the actual situation analysis, if before the target object operation is before after operation, is after Advice.
( enhanced, similar to character encoding filters )
Pointcut: The entry point, describes the application of cross-sectional features, not all processes need, those who can use is the point of entry
( similar to filter matching rules / *)
Joinpoint: The connection point, or the time when the component joins the process, such as setting properties, calling methods, and so on,Spring only supports connection points for method calls , while other frameworks support the connection points for properties such as: AspectJ,
(Filter rules similar to filter REQUEST,FORWARD)
Weave: stitching, applying a component to the process in a business process, called stitching or weaving.
(similar to the process of Configuring a filter to the Web. XML file)
Proxy, agent, in implementation,Spring 's AOP is actually using JDK Dynamic agents (which use interfaces to complete proxy operations), or you can use Cglib (which uses inheritance to complete proxy operations).
Target, destination, actual object of the business operation
Example: Setting the character encoding format is considered a aspect aspect, and the interceptor is a advice enhancement.
<!--character encoding filters--
<filter>
<filter-name>characterFilter</filter-name>
< Filter-class>com.bjpowernode.egov.filter.characterencodingfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>characterFilter</filter-name>
<url-pattern> /servlet/*</url-pattern>
</filter-mapping>
Filter class
public class Characterencodingfilter implements Filter {
@Override public
Void Destroy () {}
@Override Public
void DoFilter (ServletRequest request, servletresponse response,
Filterchainchain) throws IOException, servletexception {
request.setcharacterencoding ("GB18030");
Chain.dofilter (Request,response);
}
@Override
publicvoid init (filterconfig filterconfig) throws Servletexception {}
}
This makes it not necessary to set the encoding pull in each servlet:
the use of AOP in the spring framework 1, copy jar package
adding namespaces and constraint files to the 2,spring configuration file
Enable the AOP feature: You can pull the tag by adding it in.
3, write the class and extension classes that are being proxied
4, declared by configuration file
<!--declaring the target class--
<bean id= "Targetclass" class= "Com.spring.aop.TargetClass" ></bean>
<!-- Declaring extension classes--
<bean id= "Extendsclass" class= "Com.spring.aop.extendsClass" ></bean>
<!--weaving extension classes and declares on which method to perform the extension class-
<aop:config>
<aop:aspect id= "Extendaspect" ref= "" >
<AOP: Pointcut expression= "Execution (public * (..))" id= "Extendspoincat" >
<!--method to execute before the target method is executed--
<AOP : Before method= "Beforemethod" pointcut-ref= "Extendspoincat"/>
<!--methods executed after the target method is executed--
<AOP: After method= "Aftermethod" pointcut-ref= "Extendspoincat"/>
</aop:aspect>
</aop:config>
5, if the test succeeds, executing the target method Targetmethod () in the target class executes the Beforemethod () method in the extension class, executes the target method, and finally executes the Aftermethod () method. That is, we only need to manually invoke the Targetmethod method, the two method framework in the extension class will be executed at the time by reading the configuration file, get the corresponding information, automatically add to us the extension of the method: The test must be successful, if you don't believe it, you can try it yourself.
advantages of using AOP in the Spring framework
AOP is integrated with spring's IOC container, enhanced, and pointcuts are JavaBean and can be configured in the same file
As with other parts of spring, you can migrate between different application servers
Spring implements an AOP interception interface so that users do not have to bind to specific interceptor interfaces
AOP faces the aspect-oriented programming idea, breaking the object-oriented thinking mode, we must learn not only the use of AOP, but also to learn the aspect-oriented thinking.