"Spring Source learning" Implementation of Spring's AOP oriented slicing programming

Source: Internet
Author: User

One: Advice (notice)
(1) define what to do at the connection point and provide a weaving interface for aspect enhancement. In spring AOP, the aspect behavior that is injected around a method invocation is primarily described.
(2) Spring defines the interfaces that weave the enhanced behavior in a few moments
?? =>org.springframework.aop.beforeadvice
??? Org.springframework.aop.MethodBeforeAdvice
?? =>org.springframework.aop.afteradvice
??? Org.springframework.aop.AfterReturningAdvice
?? =>org.springframework.aop.throwsadvice

Two: PointCut (tangent)
(1) Decided that the advice should function those connection points. This means that the collection of methods that need to be enhanced is defined by Pointcut.
(2) Spring-defined Pointcut interface
?? =>org.springframework.aop.pointcut
?? =>org.springframework.aop.truepointcut
?? = Org.springframework.aop.support.JdkRegexpMethodPointcut
(3) Related interfaces. The Pointcut interface returns two objects, one classfilter, and the other is Methodmatcher
?? =>org.springframework.aop.classfilter
?? =>org.springframework.aop.trueclassfilter

?? =>org.springframework.aop.methodmatcher matches whether the current method needs to perform the enhanced logic, that is, the method that executes the advice interface.
?? =>org.springframework.aop.truemethodmatcher

Three: Notification utensils (Advisor)
?? (1) After the completion of the aspect enhancement design (advice) and the Focus design (poincut) of the target method, it is necessary for an object to combine them to accomplish this function (Advisor)
?? (2) Spring-defined advisor interface
?? =>org.springframework.aop.advisor
?? =>org.springframework.aop.support.defaultpointcutadvisor

?? (3) Interface of the advisor's controller
?? =>org.springframework.aop.framework.adapter.advisoradapter
?? =>org.springframework.aop.framework.adapter.afterreturningadviceadapter the target method finishes executing the enhanced controller
?? =>org.springframework.aop.framework.adapter.methodbeforeadviceadapter execution of the enhanced controller before the target method is executed
?? =>org.springframework.aop.framework.adapter.throwsadviceadapter the target method execution process throws an exception to execute the enhanced controller

Four: SPRINGAOP's related
?? (1) The core technology of SPRINGAOP is dynamic agent. Dynamic Agent technology is a feature of the JDK.
?? (2) SPRINGAOP proxy object Generation case
???? =>org.springframework.aop.framework.proxyfactorybean
???? =>org.springframework.aop.framework.proxyfactory


V: Proxyfactorybean Workflow.
(1) The class that implements the Factorybean interface, when it wants the IOC container to request the bean, actually returns the class to perform the return of the GetObject () method.
(2) the Proxyfactorybean GetObject () method returns the proxy object for the target object.
?? Its execution process
?? = The agent-enhanced chain is initialized first based on the property values of the configured Interceptornames. Based on the interceptornames from the IOC container, the bean that gets the enhanced class is then judged by its type to form the Defaultpointcutadvisor object.
?? = = Gets the bean of the target object from the IOC container based on the configured TargetName property value, forming a Singletontargetsource object.
?? = = The data configuration class created by itself as an AOP proxy object, creating a dynamic proxy object that is returned to the IOC container request.
(3) During the execution of the method of the target object, all the Advisor objects are found from the agent chain based on the object of the method being executed, and then the Pointcut object is used to obtain the classfilter. Methodmatcher to determine whether the current advisor is to be executed. If executed, join the execution agent chain. As an example of a JDK dynamic proxy, invoke (Object Proxy, method, object[) in the Org.springframework.aop.framework.JdkDynamicAopProxy class Args) method in this code list<object> chain = This.advised.getInterceptorsAndDynamicInterceptionAdvice (method, Targetclass);. The this.advised in this code is actually proxyfactorybean itself.

Six cases

One: the object of advice

 PackageCom.mobile.thinks.aop.advice;ImportJava.lang.reflect.Method;ImportOrg.springframework.aop.MethodBeforeAdvice; Public classLoginbeforeadviceImplementsmethodbeforeadvice{@Override Public voidBefore (method, object[] args, Object target)throwsthrowable {System.out.println ("Loginbeforeadvice.before () execution method is ==>" +method.getname ()); System.out.println ("Loginbeforeadvice.before () execution parameter is ==>" +args); System.out.println ("Loginbeforeadvice.before () execution target is ==>" +Target.getclass ()); }        }
View Code

II: Target Object

 PackageCom.mobile.thinks.service.impl;Importjava.util.Date;ImportOrg.springframework.stereotype.Service;ImportCom.mobile.thinks.entity.User;Importcom.mobile.thinks.service.UserInfoService; @Service (Value= "Userinfoserviceimpl") Public classUserinfoserviceimplImplementsuserinfoservice{@Override Publicuser Loginbyusernameandpassword (String userName, String passWord) {User User=NewUser (); User.setaddress (Sanmenxia); User.setage (28); User.setcreatetime (NewDate ());        User.setname (UserName); returnuser; }    }
View Code

Three: Configuration file

<!--test Proxyfactorybean--       <!--define a advice--       class= " Com.mobile.thinks.aop.advice.LoginBeforeAdvice "/>    <!--definition Factorybean--    class=" Org.springframework.aop.framework.ProxyFactoryBean ">        <!--the target object to be proxied, the name in the IOC container---        <property Name= "TargetName" >            <value>userInfoServiceImpl</value>        </property>        <!-- Name of the advice to be implanted in the IOC container--        <property name= "Interceptornames" >            <list><value> loginbeforeadvice</value></list>        </property>    </bean>
View Code

Seven: Spring internal implementation of dynamic agent based on the case of the way. Spring provides httpinvoker remote invocation. You can read the GetObject method of this class. Implementing the Facade Interface agent

Org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean

"Spring Source learning" Implementation of Spring's AOP oriented slicing programming

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.