I. AOP and interceptors
In some cases, AOP and interceptors, including filter, can achieve the same function, usually the request is the controller layer of operations, these three execution order is FILTER>INTERCEPTOR>AOP, of course, the difference I will be re-write an article to explain, This is to tell you that you do not have to use AOP, and that personal feeling is easier and simpler to implement with filter and interceptor.
Two. AOP Preparation
In the spring framework, you'll also need to add the relevant jar packages for Aspectjrt,aspectjweaver and Cglib, and the MAVEN project's Pom.xml can be configured directly in MAVEN public service.
Configure AOP in Spring-mvc.xml and add <aop:aspectj-autoproxy/>.
For the Aspect class, add @aspect and @component to the class.
three. Spring AOP simplified Medium
The concept of AOP to look at the document is more abstract, the online example is basically the operation of the log these, but the log record is indeed a very image of the example. Plainly, when we want to do some complementary operations on some batch methods, we generally choose AOP in the service layer and the DAO layer.
Log example, when we are in the DAO layer when we execute crud and want to generate a log record in the database, this time the log processing code is actually the same, we can extract it.
For example, some operations need to send short-notice users (Change password, bill expires, etc.), the text message is the same code, is a mobile phone number and text message content. We can solve this problem with AOP, and all the methods that need to be texted can be solved with AOP. The advantage is that you only need a place to maintain the code, dynamically decide where to send text messages to the user.
AOP is generally used when we are all with a number of annotations to complete, the main role of annotations are two aspects: 1. Can be used to determine whether to intercept the method, 2. Pass some parameters or variable values that are required in the AOP method.
Glossory of AOP:
1.Pointcut: What methods AOP intercepts, must tell the AOP method, generally we are through a similar regular expression, Pointcut have different types, usually we see is the execution, In fact, I currently use execution, other types such as: Within,this,args,target, @args, @target, @annotation ...
Here are a few examples of pointcut: Pointcut expressions can be ' | | ', ' && ' and '! ' Connection
@Pointcut ("Execution (pubic * * (..))")
private void Alloperation () {}
@Pointcut ("Within (Com.cnblog.service.User.)") = @Pointcut ("Execution (* com.cnblog.service.user.* (..))")
private void Useroperation () {}
@Pointcut ("Alloperation () && useroperation ()")
private void Unionoperation () {}
2.Advice: Advice is associated with a pointcut expression, and runs before, after, or around method executions Ma Tched by the pointcut. The pointcut expression either a simple reference to a named Pointcut, or a pointcut expression declared in Place.
Our usual simplest usage is @around ("Execution (public *.update* (..))"), which is actually equivalent to @around (pointcut= "Execution (public *.update* (..))") , the value of pointcut can be a function of the pointcut expression we defined above. The following are mainly about four kinds of advice:
Before: The execution of the interception function before execution, general data permissions or user authentication information is the case, similar to the Interceptor Prehandle.
@Before ("Execution (public * * (..))")
public void Docheck () {...}
Around:around is the most commonly used, because it can be fused before and after, and can determine when the intercepted method is executed, advice The first parameter of method must be proceedingjoinpoint. The intercepted function can be executed by invoking the proceed method of the Proceedingjoinpoint.
@Around ("Execution (public * * (..))")
Public Object doaround (proceedingjoinpoint pjp,..) {
object[] args = Pjp.getargs ();
...
Return pjp.proceed (args);
}
After: Executes after the function executes, executes to contain the intercepted function return value or the exception, the general situation we are for the exception to make a record and so on.
@After ("Execution (public * * (..))")
public void Dofinal () {...}
Note: Advice ordering: If more than one advices is executed at the same connection point, then there is an order of execution, and generally we can avoid it if we can avoid it by @order ( Org.springframework.core.annotation.Order) to indicate the order of precedence. The higher the priority, the "on the the" is executed first, and "on the" is executed after.
4.Other keys: introductions,perthis,pessimisticlockingfailureexception (Details refer to spring official documentation)
Four. Choice of AOP
Spring AOP or AspectJ, Aspect language style, @AspectJ or Spring XML style (Aspect schema_based here, is the XML configuration of AOP).
1.Spring AOP or Full AspectJ
Spring AOP is simpler because ASPECTJ also needs to introduce compiler/weaver-related jar packages.
If your AOP is in spring beans, you can use spring AOP directly, and if you want to intercept objects that are not spring container managed only using ASPECTJ, the individual has been using ASPECTJ because it is more convenient if It's easier to use Maven's words.
2. Personal advice to use the @aspect syntax style directly, using the spring XML configuration is cumbersome and easy to omit.
Five. AOP Proxy
Spring AOP uses the JDK dynamic agent or Cglib to create a proxy for the target object, which is preferred by the JDK dynamic agent.
By default: If the target object of the agent implements at least one interface, then the JDK dynamic Proxy is used by default, and Cglib is used instead.
Of course, you can also force the use of cglib, if you prefer it, but in spring3.x you first have to provide cglib related jar packages. You then need to configure <aop:config proxy-target-class= "true" in spring/>,to Force CGLIB proxying when using @AspectJ autoproxy Suppo RT ==> <aop:aspectj-autoproxy proxy-target-class= "true"/>
Understanding AOP Agents:
Generic Object invocation: interface Pojo, class Simplepojo implements Pojo
Public Classmain { public static Voidmain (string[] args) { Pojo Pojo = new Simplepojo (); This was a direct method call on the ' Pojo ' reference Pojo.foo (); }}
Let's change it a little bit, when the client has only one agent,
Public Classmain {public static voidmain (string[] args) { Proxyfactory factory = newproxyfactory (Newsimplepojo ()); Factory.addinterface (pojo.class); Factory.addadvice (Newretryadvice ()); Pojo Pojo = (Pojo) factory.getproxy (); This was a method call on the proxy! Pojo.foo (); }}
Spring AOP with AspectJ