AOP operations in spring (dynamic proxy) _spring

Source: Internet
Author: User
Tags aop modifier
Aop

1.AOP Overview
2.AOP Bottom Principle
3.AOP Operational Terminology AOP Concepts

AOP: Aspect-oriented programming, the simple understanding is that extended functionality can be implemented without modifying the source code. AOP takes the horizontal extraction mechanism, replaces the traditional vertical inheritance system repetitive code. (Performance monitoring, transaction management, security checks, caching) the underlying principles of AOP:



AOP Operations-related terminology:

Joinpoint (Connection point): The so-called connection point refers to the points that are intercepted. It's also a time to add business-independent features to the business, and these include: calling a method, calling a property, throwing an exception three time. In spring, these points refer to invoking the method timing, because spring only supports connection points for method types.

Pointcut (cut-in point): The so-called entry point is where we want to joinpoint to intercept the definition. The method we specify to enhance is called the pointcut.

Advice (notice/enhancement): the so-called notice is to intercept to Joinpoint after the thing is to do is notice, the notice is divided into front notice, post notice, abnormal notice, Final notice, surround the notice (section to complete the function). That is, enhanced logic, called enhancements, such as extended log functionality, is called enhancement. Predecessor notification: Performing a post notification before a method: Performing an exception notification after the method: The method has an exception final notification: Execute wrapping notification after the post: Before and after the method

Aspect (Slice): A combination of pointcuts and notifications (referrals). The process is called the cutting surface by applying the reinforcement to the concrete method.

Introduction (Introduction): Introduction is a special kind of notice Introduction can dynamically add methods or field to a class at run time without modifying the class code.

Target (target object): The object of the proxy (the class to be enhanced)

Weaving (weaving): The process of applying an enhancement to a target. The process of applying advice to target.

Proxy: When a class is woven into the enhancements by AOP, a result proxy class is generated.

corresponding execution order underlying structure:

try
{
    //  execute forward notification;//execute  target method;//execute

    return notification;
}
Catche (Exception e)
{/
    /execute exception notification;
}
finally
{
    //execute post notification;
}
Spring's AOP operation is an AOP operation in spring, using aspect to implement aspect is not part of spring, and use it with spring for AOP operations aspect Introduction

1.Aspect is a facet-oriented framework that extends the Java language. Aspect defines the AOP syntax, so it has a specialized compiler for generating class files that follow Java byte encoding specifications.
2.Aspect is an AOP framework based on the Java language
3.spring2.0 later added support for aspect pointcut expressions
4. @Aspect is the Aspect 1.5 new feature that allows you to define slices directly in the bean class by JDK5 annotation technology
5. New version of the spring framework, recommend using the aspect approach class to develop AOP
6. Use aspect to import Spring AOP and aspect related jar packs

Spring AOP (also spring's JavaBean annotation Management class jar package, but introduces xmlns:context= "Http://www.springframework.org/schema/context" in XML) This constraint, if it is directly using AOP Dynamic proxy injection method, there is no need to introduce the constraints above but there is xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" is enough, Two annotations the same place is to introduce AOP this jar package, now do not understand, to the later with the AOP annotation injection method to know the download address
ASPECTJ related Jar Pack downloads use ASPECTJ to implement AOP in two different ways:

1. XML configuration based on aspect
2. Aspect annotation approach based on ASPECTJ XML configuration AOP Operational readiness:

1. In addition to importing basic jar packages, you need to import AOP-related jar packs
2. Create spring's core configuration file, import AOP constraints

<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 "
    xmlns:context=" http:// Www.springframework.org/schema/context "xsi:schemalocation="
        Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd<!--constraints on Javaeean Management to implement--> HTTP via XML configuration
        ://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd<!-- On the constraint--> http://www.springframework.org/schema/context http://www.springframework.org/schema/of AOP dynamic agents
        Context/spring-context.xsd "><!--about Javaeean management constraints that are implemented by annotations, i.e. automatically create JavaBean after adding annotations, but this annotation is not an annotation of an AOP dynamic proxy. This is not needed when using AOP dynamic proxy annotations, which requires the above AOP constraint. -->
The above constraint code can be found in the following ways:

Start looking from the bottom.

Using an expression to configure Pointcuts

1. Implement spring Creation object to spring management through configuration
2. Configuration pointcut: The actual enhanced approach
3. Configuration pointcuts commonly used expressions: execution (< access modifier >?< return type >< method name > (< parameter >) < exception >) Execution (* Cn.domarvel.aop.Book.add (..)) Where: the first * means any modifier, "..." Indicates that there are parameters or no parameters. An expression means an enhanced execution (* cn.domarvel.aop.book.* (..)) of the Add method inside a class Cn.domarvel.aop.Book. Indicates that all methods within the class are enhanced execution (* *.* (..)) All methods of all classes are enhanced to match all save start Methods Execution (* save* (..)) Execution (* Cn.domarvel.service ... *.* (..)), matching all the methods of all classes under the service. Example: Predecessor enhancements

Classes to be enhanced:

public class Langdao {public

    void Showlangdao () {
        System.out.println ("Langdao");
    }

Enhanced classes:

public class Strenglangdao {public

    void Strenglangdao () {
        System.out.println ("predecessor enhanced Langdao");
    }

Implementation enhancements through configuration:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "Http://www.springframework.org/schema/aop" xmlns: context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ AOP http://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/context http:/
/www.springframework.org/schema/context/spring-context.xsd > < Configure--> <!--Configuration object in the spring core configuration file--> <bean id= "Langdao" class= "Cn.domarvel.dao.LangDao" ></bean> <bean id= "Strenglangdao" Cn.domarvel.dao.StrengLangDao ></bean> <!--Configure AOP operations--> <aop:config> <!--configuration Pointcut--> ; Aop:pointcut expression= "Execution (* cn.domarvel.dao.LangDao.showLangdao (..)) "
        Id= "Pointcutlangdao"/> <!--configuration cut: Use the enhancements above the method--> <aop:aspect ref= "Strenglangdao" > <!--configuration Enhancement Type: Method enhanced class where to use as a--> <aop:before method= "Strenglangdao" pointcut -ref= "Pointcutlangdao"/> </aop:aspect> </aop:config> </beans>

Test code:

public class Langdaotest {

    @Test public
    void Showlangdaostreng () {
        ApplicationContext context=new Classpathxmlapplicationcontext ("Applicationcontext.xml");
        Langdao langdao= (Langdao) Context.getbean ("Langdao");
        Langdao.showlangdao ();
    }
/* Output:

front enhanced Langdao
Langdao
* *
Both the predecessor enhancements and the post enhanced configuration and usage are similar.

What needs to be changed is:

Predecessor: <aop:before method= "Strenglangdao" pointcut-ref= "Pointcutlangdao"/>
Post: "'

We don't need to change the other classes, just change the profile information:
Overall configuration:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "Http://www.springframework.org/schema/aop" xmlns: context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ AOP http://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/context http:/
/www.springframework.org/schema/context/spring-context.xsd > < Configure--> <!--Configuration object in the spring core configuration file--> <bean id= "Langdao" class= "Cn.domarvel.dao.LangDao" ></bean> <bean id= "Strenglangdao" Cn.domarvel.dao.StrengLangDao ></bean> <!--Configure AOP operations--> <aop:config> <!--configuration Pointcut--> ; Aop:pointcut expression= "Execution (* cn.domarvel.dao.LangDao.showLangdao (..)) "
        Id= "Pointcutlangdao"/> <!--configuration cut: Use the enhancements above the method--> <aop:aspect ref= "Strenglangdao" >  <!--configuration Enhancement Type: Method enhanced class where to use as--> <aop:after-returning method= "Strenglangdao"
pointcut-ref= "Pointcutlangdao"/><!--Note: Here the Aop:after tag is also able to execute--> </aop:aspect> </aop:config> </beans>
The difference between aop:after-returning and Aop:after is:

aop:after-returning: The

enhanced method does not perform aop:after when an exception is added to the enhanced method

:

The Post method is executed regardless of whether the enhanced method succeeds or

not. You can look at the order in which the previous enhancement methods were executed.
Example: Surround enhancement

Enhanced classes:

public class Langdao {public

    void Showlangdao () {
        System.out.println ("Langdao");
    }

Enhanced classes:

public class Strenglangdao {public

    void Strenghuanraolangdao (Proceedingjoinpoint proceedingjoinpoint) {
        System.out.println ("before Method");
        try {
            proceedingjoinpoint.proceed ();
        } catch (Throwable e) {
            e.printstacktrace ();
        }
        System.out.println ("after Method");
    }

Spring Core configuration file:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "Http://www.springframework.org/schema/aop" xmlns: context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ AOP http://www.springframework.org/schema/aop/spring-aop.xsd Http://www.springframework.org/schema/context http:/
/www.springframework.org/schema/context/spring-context.xsd > < Configure--> <!--Configuration object in the spring core configuration file--> <bean id= "Langdao" class= "Cn.domarvel.dao.LangDao" ></bean> <bean id= "Strenglangdao" Cn.domarvel.dao.StrengLangDao ></bean> <!--Configure AOP operations--> <aop:config> <!--configuration Pointcut--> ; Aop:pointcut expression= "Execution (* cn.domarvel.dao.LangDao.showLangdao (..)) "
        Id= "Pointcutlangdao"/> <!--configuration cut: Use the enhancements above the method--> <aop:aspect ref= "Strenglangdao" > <!--configuration Enhancement Type: Method Enhancement class where to use to surround--> <aop:around method= "Strenghuanraolangdao" p ointcut-ref= "Pointcutlangdao"/> </aop:aspect> </aop:config> </beans>

Test code;

public class Langdaotest {

    @Test public
    void Showlangdaostreng () {
        ApplicationContext context=new Classpathxmlapplicationcontext ("Applicationcontext.xml");
        Langdao langdao= (Langdao) Context.getbean ("Langdao");
        Langdao.showlangdao ();
    }
Output:/
*
method before
Langdao
method
* *

AOP Personal Understanding

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.