Spring Note 4-AOP

Source: Internet
Author: User

Introduction to AOP:

Plane-oriented programming

First, we introduce dynamic agents:

Dynamic proxies can add business logic before all methods of the class. The classes in the JDK want to be dynamic proxies, and interfaces must be implemented (assuming target implements interface ITarget). Use proxy and implement Invocationhandler interface to implement dynamic proxy (Video: Dynamic proxy). The Phandler implements the Invocationhandler interface, which contains the private object (target). Write your own pre-proxy business logic in your own Invoke method (see reflection), then invoke the corresponding method of the Proxied object (i.e., Method.invoke (Target,args)) through the reflection method, and finally add the post-agent business logic. If you add different business to different business logic, you can pass the method into Phandler Beforemethod (), and then judge different methods to write different Beforemethod method content.

The process of implementing a dynamic proxy is to generate a proxy object target, generate a phandler, and pass the target to Phandler with a setter. Then, using itarget targetproxy = Proxy.newproxyinstance (), target and handler are passed in, and the incoming method is Arg0:target.getClass (). getClassLoader () , Arg1:target.getClass (). Getinterfaces (), Arg2:phandler. Handler contains the business logic that we want to join, target is the proxy object and contains the original business logic.

Finally executes the targetproxy. Business logic (business parameters), executes the handler begin logic first, executes the target, and finally executes the handler end logic.

The implementation of AOP in spring is based on the implementation of the dynamic proxy (for classes that have implementations) and changes the binary code directly to classes that do not implement the interface.

Implementation of AOP:

Implementation of AOP in Java requires the addition of xmlns= "HTTP://.../AOP" in Spring's. xml, Xsi:schemalocation plus schema/aop,schema/aop/ Spring-aop-2.5.xsd (You can look for a routine in spring samples, or dist/resources find an AOP. xsd) (add the following. xsd to the IDE's XML catalog to get the IDE's hint function). Add <aop:aspectj-autoproxy/> to the. XML body, and you can use the @aspectj tag (the program scans the @aspectj tags and makes dynamic proxies).

ASPECTJ:ASPECTJ is a project dedicated to generating dynamic agent functionality. Spring also uses ASPECTJ for AOP implementations. Two. jar packages to introduce ASPECTJ when using AJ.

Use of AOP:

Add a @aspect tag before the class name (indicating that this class can be woven as a tangent) and join @component (which will be given to spring to initialize for weaving work). By adding @before tags (or any advice) above the business logic that you want to weave before the original logic (such as Beforemethod ()) (in fact, there are classes, attributes, and so on), it is possible to weave beforemethod () into the original object logic.

Syntax for @Before:

@Before ("Execution (Public return value type Com.company.dao.impl.UserDAOImpl. function name (com.company.model. function parameter class name)") : Weave the method described by @before into the function name described by execution before execution. Execution is a special keyword for method weaving.

Joinpoint:

Pointcuts, that is, which points in the original logic need to be woven into the facet logic, that is, the position that is woven into the original logic when a slice is inserted in the previous business logic.

PointCut:

A collection of pointcuts.

@Pointcut ("Execution (* com.company.someapp.service.*.* (..))")

public void SomeMethod () {}

Any method that represents any class under Com.company.someapp.service is a pointcut for SomeMethod ().

SomeMethod can be an empty implementation, as a parameter of advice. You can then write SomeMethod () in any advice parameter and execute it in this Pointcut collection.

Advice:

Represents the proposed logic on the facets. such as @before, @After are the point of entry suggestions.

Target:

The object that is being proxied (woven into the object).

Weave:

Woven into.


ASPECTJ syntax supported by AOP:

Execution (Public * * (..)) : Any method that exposes any return value

Execution (* set* (..)) : Any method that returns a value that has the name set to begin with

Execution (* com.comp.service.*.* (..)) : Any method of any class under any service package that returns a value

Execution (* com.comp.service). *.*(..)) : Any method that returns the value of any class under the service package and its child packages (infinite layers)

AOP self-made syntax:

Within (com.comp.service.*):

Within (Com.comp.service. *):

This (Com.comp.service.SomeService):

Target (Com.comp.service.SomeService):

Args (java.io.Serializable):

etc.


Advice Type:

@before ("Execution ()") or before (SomeMethod) [see above pointcut explanation]:

@AfterReturning: Executes after the target method returns to normal.

@AfterThrowing: Executes after the target method throws an exception.

@After: The Finally, whether abnormal or normal return is performed.

@Around: Surround. The wrapping method public Object dobasicprofiling (the method name is convertible), the parameter is Procedingjoinpoint PJP (not convertible), throws Throwable.

In this method, first write the Before weaving logic, then write the object retVal = Pjp.proceed (), and then write the after weaving logic, and finally return retVal;. (Video with responsibility chain mode)

Advice parameters can be execution, or somemethod[see pointcut explanation]

Spring Note 4-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.