Spring Framework Learning (vi) AOP

Source: Internet
Author: User

Spring Framework Learning (vi) AOP

AOP (Aspect-orientedprogramming) aspect-oriented programming, unlike OOP, is divided into aspects or concerns, rather than oop , using an AOP programming system. The object in the.

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 in the judgment, according to the appropriate permissions to execute the appropriate method; When you set the encoding format in the servlet , the same code appears many times, but also the root of the business is irrelevant, it is easy to forget to write, the results of the run when the garbled pull. 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, invoking methods, and so on,Spring only supports connection points for method calls , Some other frameworks support the connection points for attributes 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 (using interfaces to complete proxy operations) , you can also use CGLIB(the proxy operation is done using inheritance).

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.

[HTML]View Plaincopyprint?
  1. <span style="FONT-SIZE:18PX;" ><!--character encoding filter --
  2. <filter>
  3. <filter-name>characterfilter</filter-name>
  4. <filter-class>com.bjpowernode.egov.filter.characterencodingfilter</filter-class >
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>characterfilter</filter-name>
  8. <url-pattern>/servlet/*</url-pattern>
  9. </filter-mapping>
  10. </span>

Filter class

[Java]View Plaincopyprint?
  1. <span style="FONT-SIZE:18PX;" >public class Characterencodingfilter implements Filter {
  2. @Override
  3. public Void Destroy () {}
  4. @Override
  5. public void DoFilter (ServletRequest request, servletresponse response,
  6. Filterchainchain) throws IOException, servletexception {
  7. Request.setcharacterencoding ("GB18030");
  8. Chain.dofilter (Request,response);
  9. }
  10. @Override
  11. Publicvoid Init (filterconfig filterconfig) throws servletexception {}
  12. }</span>

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  

2. add namespaces and constraint files to the 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

[HTML]View Plaincopyprint?
  1. <span style="FONT-SIZE:18PX;" ><!--declaring a target class --
  2. <Bean id="Targetclass" class="Com.spring.aop.TargetClass"></ Bean>
  3. <!--declaration Extension class--
  4. <Bean id="Extendsclass" class="Com.spring.aop.extendsClass"></Bean >
  5. <!--weaves the extension class and declares on which method to perform the extended class--
  6. <aop:config>
  7. <aop:aspect id="Extendaspect" ref= "">
  8. <aop:pointcut expression="Execution (Public * * (..))" id="Extendspoincat">
  9. <!--the method to execute before the target method executes--
  10. <aop:before method="Beforemethod" pointcut-ref="Extendspoincat" />
  11. <!--How to execute after the target method executes--
  12. <aop:after method="Aftermethod" pointcut-ref="Extendspoincat" />
  13. </aop:aspect>
  14. </aop:config></span>



5, test If successful, 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 JavaBeanand 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.

Spring Framework Learning (vi) AOP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.