Automatic proxy generator for SPRING8--AOP beans

Source: Internet
Author: User
Tags throwable

  For the two issues mentioned at the end of the previous blog http://www.cnblogs.com/cdf-opensource-007/p/6464237.html, you can use the automatic proxy generator provided by spring to resolve. The automatic proxy generator allows us to weave slices into the target object method without using the Proxyfactorybean class to generate proxy objects, and it is possible to weave the facets into multiple proxy proxy objects, and use the ID of the target object to obtain the proxy object, which is in line with the normal usage habits.

Spring provides two automatic proxy generators, one of which is defaultadvisorautoproxycreator, a proxy generator that is more violent, which weaves all the facets of a container into all target objects, and the facets can only be the advisor's form. Another automatic proxy generator is beannameautoproxycreator, which is a very friendly code generator that allows us to selectively weave objects and facets into the form of a consultant or a notification, as well.

Here we still explain the problem in the form of experiments. Provide two interfaces respectively, and two implementation classes for target object creation, on the basis of the previous blog experiment we added a comparator interface and implementation class.

Calculator interface:

 Public Interface Icalculatorservice {        int Add (int A,int  b);         int Division (int A,int  b);}

Comparator interface:

 Public Interface Icomparatorservice {        void Comparator (int A,int  b);}

Calculator Implementation class:

 Public class Implements Icalculatorservice {    @Override    publicint Add (intint  b) {         return A +b;    }    @Override    publicint Division (intint  b) {         return A/b;    }}

Comparator Implementation class:

 Public classComparatorserviceimplImplementsIcomparatorservice {@Override Public voidComparatorintAintb) {if(A >b) {System.out.println (a+ "bigger" than "+b+"); }Else if(A <b) {System.out.println (a+ "smaller" than "+b+"); }Else{System.out.println (a+ "equals" +b); }    }}

Still provides three facets of the implementation class, respectively, is the pre-notification, surround notification and post-notification.

 Public class Implements Methodbeforeadvice {    @Override    publicvoidthrows  throwable {        System.out.println ("Execute the Pre-notification--->" + "The method being executed is named" +Method.getname ());}    }

 Public classTestmethodinterceptorImplementsMethodinterceptor {@Override PublicObject invoke (Methodinvocation invocation)throwsthrowable {System.out.println ("Perform surround Notification--->" + "executing method named" +Invocation.getmethod (). GetName ()); object[] Arguments=invocation.getarguments (); intA = (int) arguments[0]; intB = (int) arguments[1]; if(b = = 0) {System.err.println ("Divisor cannot be 0"); return-1; }        if(A = = 0){            return0; }                        returninvocation.proceed (); }}

 Public class Implements Afterreturningadvice {    @Override    publicvoidthrows  Throwable {                System.out.println ("execute post Notification--->" + "executing method named" +Method.getname ());}    }

Configuration file:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:task= "Http://www.springframework.org/schema/task"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd                    Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd                    Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd                    HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd                    Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">            <!--target Object -                <BeanID= "Comparatorservicetarget"class= "Com.opensource.service.impl.ComparatorServiceImpl"/>    <BeanID= "Calculatorservicetarget"class= "Com.opensource.service.impl.CalculatorServiceImpl"/>    <!--Notice -    <BeanID= "Methodbeforeadive"class= "Com.opensource.service.impl.TestMethodBeforeAdive"/>    <BeanID= "Afterrunningadive"class= "Com.opensource.service.impl.TesAfterRunningAdive"/>    <BeanID= "Methodinterceptor"class= "Com.opensource.service.impl.TestMethodInterceptor"/>    <!--Consultant -    <BeanID= "Advisor"class= "Org.springframework.aop.support.NameMatchMethodPointcutAdvisor">        < Propertyname= "Advice"ref= "Methodinterceptor"/>        < Propertyname= "Mappednames"value= "Division"/>    </Bean>    <Beanclass= "Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">        < Propertyname= "Beannames">            <List>                <value>Calculatorservicetarget</value>                <value>Comparatorservicetarget</value>            </List>        </ Property>        < Propertyname= "Interceptornames">            <List>                <value>Methodbeforeadive</value>                <value>Afterrunningadive</value>                <value>Advisor</value>            </List>        </ Property>    </Bean>                </Beans>

Test class:

 Public classMyTest { Public Static voidMain (string[] args) {ApplicationContext ac=NewClasspathxmlapplicationcontext ("Spring-bean.xml"); Icalculatorservice Bean= (Icalculatorservice) ac.getbean ("Calculatorservicetarget"); Icomparatorservice Bean1= (Icomparatorservice) ac.getbean ("Comparatorservicetarget"); intDivision = bean.division (10, 0); System.out.println ("Two number of dividing quotient:" +division); System.err.println ("---------------------------------------------------------"); intAdd = Bean.add (0, 2); System.out.println ("Two numbers want to add and for:" +add); System.err.println ("---------------------------------------------------------"); Bean1.comparator (0, 1); }}

Experimental results:

Here is a sentence: for beannameautoproxycreator in beannames and interceptornames These two properties can be used in a fuzzy matching way, For example, the following is the form:

<name= "Beannames"  value= "*target"/>

There is also a consultant packaging notification when an advisor can only wrap a notification, which also reflects the consultant's fine-grained management of notifications.

To conclude, we, as programmers, are going to go into a little bit of a thorough study of the problem. When you have a thorough understanding of the principle, development is also handy, a lot of development problems and doubts will be solved, and in the face of other problems can also be done comprehend by analogy. Of course, in the development of not too much time to let you study the principle of development in order to achieve the premise of the function, you can wait for the project on-line, you have a lot of time or spare time, you can go to the inquisitive, in-depth study of a technology, in order to think this is a programmer's growth is very important thing.

Automatic proxy generator for SPRING8--AOP beans

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.