Dead Knock Spring AOP Series 1: Programmatic implementation of AOP

Source: Internet
Author: User
Tags throwable

As the first of the "Dead Spring AOP" series, this series is an AOP source analysis level article. Since AOP is no longer an advanced technology, there are also examples of Web-based XML schemas and annotation declarations. I believe that using spring's friends can be handy.

The principles of this series of articles

    1. How to configure AOP is not the focus

    2. AOP-related concepts are not the focus of explanation

    3. AOP underlying code design is the focus

The main content of this article

    1. Know Proxyfactory, and through the factory class, "Log" and "Security check" code into the business logic

    2. Analyze the implementation process of the proxy object.

    3. Recognize Proxyfactory related objects



1. Using Proxyfactory to achieve the "AOP" effect

/** * Analog Service Interface  */public interface userservice {    public void  updateuser ();} /** * simulation of specific business  */public class UserServiceImpl implements UserService{      @Override     public void updateuser ()  {         system.out.println ("$$$$$$ Execution business logic $$$$$");    }}/** *  Analog Tangent 1 */public class securityinterceptor implements methodinterceptor {      @Override     public object invoke (methodinvocation  Methodinvocation)  throws Throwable {         System.out.println ("========== Perform security check ====================");         Return methodinvocation.proceed ();    }}/** *  analog facets 2 */public  Class loggerbeforeadvice implements methodbeforeadvice {     @Override      Public void before (Method method, object[] args, object target)  throws  throwable {        system.out.println ("======= Save update log ======= = = ");     }}


Main class

public static void Main (string[] args) {//1. Initialize the source object (be sure to implement the interface) UserService target =new Userserviceimpl ();    2.AOP Agent Factory Proxyfactory pf = new Proxyfactory (target);    3. Assemble advice Pf.addadvice (new Securityinterceptor ());    Pf.addadvice (New Loggerbeforeadvice ());    Pf.addadvisor (New Defaultpointcutadvisor (New Loggerbeforeadvice ()));    4. Get proxy object UserService proxy = (userservice) pf.getproxy (); 5. Call the business Proxy.updateuser ();}

Output results

========== performing a security check ====================
======= Saving the update log =========
$$$$$$ Executing business logic $$$$$


The result confirms that the effect of AOP cut-in is realized by means of programming. Logic is simple, and there is no need to explain too much. Put the emphasis on the Proxyfactory object. Below, focus on the next proxyfactory.

2. Analysis Proxyfactory

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/82/4B/wKioL1dQR2HAcFXEAABux8L80Z4236.png "title=" Factory.png "alt=" Wkiol1dqr2hacfxeaabux8l80z4236.png "/>


Proxyfactory inherits the Proxycreatorsupport class, holds the Aopproxyfactory,advisorchainfactory,

List<advisor>. such as


Class
Role
Aopproxyfactory Generates an AOP proxy object based on the Advisedsupport configuration information. Default = "defaultaopproxyfactory"
Advisorchainfactory Advisor chain. The default implementation is Defaultadvisorchainfactory, and there is only one way to get the Methodinterceptor list or Dynamicinterceptionadvice list that advised meets the criteria
Advisor Holds AOP advice and filter
Proxyconfig
To generate configuration meta information for a proxy object

3.AOP Key Interface Hodgepodge

3.1advice&Interceptor

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/82/4C/wKiom1dQSz7D--fAAAAy0HpjvGI119.png "title=" Advice.png "alt=" Wkiom1dqsz7d--faaaay0hpjvgi119.png "/>

Interceptor VS Advice

Advice is an aspect of AOP programming (Aspect) that performs a specific action at a connection point (Joinpoint), which can be customized In spring, interceptor more attention to the actions of certain connection points (property access, object constructs, method calls) while the program is running. To be exact, the range of interceptor is narrower.

3.2 Pointcut&advisor

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/4C/wKiom1dQTUuwsTKjAABGdxR8uqc708.png "title=" Pointcut&advisor.png "alt=" Wkiom1dqtuuwstkjaabgdxr8uqc708.png "/>

Concept List

Terms Description
Aspect
A module which has a set of APIs providing cross-cutting requirements. For example, a logging module would is called AOP aspect for logging. An application can has any number of aspects depending on the requirement.
Join Point This represents a-your application where you can plug-in AOP aspect. You can also say, it's the actual place in the application where an action would be taken using Spring AOP framework.
Advice This was the actual action to being taken either before or after the method execution. This is actual piece of code, invoked during program execution by Spring AOP framework.
Pointcut This was a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns as we'll see in our AOP examples.
Introduction An introduction allows the add new methods or attributes to existing classes.
Target Object The object being advised by one or more aspects and this object is always a Proxied object. Also referred to as the advised object.
Weaving Weaving is the process of linking aspects with other application types or objects to create an advised object. This can is done at compile time, load time, or at runtime.

3.4proxy&proxyfactory

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/82/4C/wKiom1dQTsySiRwNAAAjgjDsxmo266.png "title=" Proxy.png "alt=" Wkiom1dqtsysirwnaaajgjdsxmo266.png "/>

4. Proxyfactory bottom-level implementation design

Figuring out the underlying implementation of proxyfactory, and basically figuring out how AOP works, regardless of whether you use XML Schema or annotation, the core is here. The main difference is only the use of pointcut differences.


Advisor is the core element of the design, plays a connecting role, connecting the advice and pointcut. The default implementation is: Defaultpointcutadvisor. Need to know 2 interface Classfilter,methodmatcher, through the method name, you can determine their role, mainly used in the "match".

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/82/4C/wKiom1dQT87z01lAAACpJxJJ9j4702.png "title=" Designer.png "alt=" Wkiom1dqt87z01laaacpjxjj9j4702.png "/>


Sequence diagram

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/82/4C/wKiom1dQT_uhGJ82AADfJFuvcQQ570.png "title=" Seq.png "alt=" Wkiom1dqt_uhgj82aadfjfuvcqq570.png "/>

With this sequence diagram, you can clearly understand the invocation process of the agent. The whole process is to construct a matching element + reflection call around a core. If you want to understand the underlying proxy invocation implementation, you can see org.springframework.aop.framework. Reflectivemethodinvocation.


Next, you will analyze how the Spring Bean factory generates the agent Bean, taking the Defaultadvisorautoproxycreator class as an example.


This article is from a "simple" blog, so be sure to keep this source http://dba10g.blog.51cto.com/764602/1785667

Dead Knock Spring AOP Series 1: Programmatic implementation of 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.