Spring Aop implements function interior enhancement __ function

Source: Internet
Author: User
Tags aop

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.

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.