One aspect of Spring Aop includes 1. Enhanced types (before and after function calls, throwing exceptions and before and after function calls) 2. Enhanced business logic 3. Connection point
Spring Aop only supports connection points for methods, and when I want to weave enhancements inside a method, there are two ways to do it:
1. For example, a function A has a,b,c three functions, I would like to be in the middle of a function before B also weave an enhancement, then you can separate B to write a separate function, and then call B in a, so you can b before and after weaving into the plane.
2. The general function has only one function and is difficult to split. For example, there is a variable a in the middle of the function, and now I want to weave an enhancement after this variable, and pass the variable over, then I can define an empty function in the class (nothing) as the target function, then assign the variable A to the function's formal parameter, and then weave the tangent plane into the function. Then this facet can get the parameter that the target function passed in, a. The main function calls this empty function.
For example, in my function, I want to pass the variable getcount and sumcount to the tangent plane (assuming the function of the slice is to print the variable)
public void Run () {when
(true) {
query query = Session.createquery ("from Vehicleinfo");
Query.setfirstresult (GetCount * getnumonetime);
Query.setmaxresults (getnumonetime);
list<vehicleinfo> infolist = Query.list ();
getcount++;
Sumcount + = Infolist.size ();
To weave a slice here and pass the variable to the slice
}
}
I can define an empty function and do nothing.
public void Transfertoweb (int getcount,int sumcount) {}
Then, before or after this function, the tangent plane is woven into the slice, and then the empty function is called in the Run function.
getcount++;
Sumcount + = Infolist.size ();
Transfertoweb (Getcount,sumcount);
However, it is important to note that AOP needs to be proxied only if the proxy class calls the Transfertoweb enhancement directly, and the transfertoweb is called indirectly through the run function, because the agent is not passed, so the enhancement will not be woven into it. So here we have to force the proxy for that class, for example, I'm the class name of convert, then change the code to
Sumcount + = Infolist.size ();
Get Agent
(Convert) Aopcontext.currentproxy ()). Transfertoweb (Getcount,sumcount);
Enhanced to weave in.