Spring AOP uses grooming: automatic proxy and AOP command space

Source: Internet
Author: User

Third, the realization of automatic agent

1. Using Beannameautoproxycreator

The agent Bean is automatically generated by the bean's Name property.

<Beanclass= "Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">    < Propertyname= "Beannames">        <List>            <value>*target</value> <!--The bean whose name ends with Target will be intercepted -        </List>    </ Property>    < Propertyname= "Interceptornames">        <List>            <value>Personaroundadvice</value>        </List>    </ Property> </Bean>

2. Using Defaultadvisorautoproxycreator

Defaultadvisorautoproxycreator allows us to complete automatic proxies by simply defining the appropriate advisor Notifier. Once the Defaultadvisorautoproxycreator managed bean is configured, it automatically finds the advisor defined in the configuration file and acts on all the beans.

Spring provides the following notifier:

Regexpmethodpointcutadvisor

Namematchmethodpointcutadvisor

<Beanclass= "Org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/><BeanID= "Namematchmethodpointcutadvisor"class= "Org.springframework.aop.support.NameMatchMethodPointcutAdvisor">    < Propertyname= "Advice"ref= "Personaroundadvice"/>    < Propertyname= "Mappednames">        <List>            <value>*info*</value>        </List>    </ Property></Bean>

3. Test code

New Filesystemxmlapplicationcontext ("Classpath:com/cjm/aop/beans.xml"= (person) Context.getbean (" Persontarget ");  // The specific Bean is obtained here, not the agent Beanp.info ();

Iv. AOP based on the AOP namespace

1, cutting-type Myaspect source code

 Public classMyaspect { Public voidBeforeadvice (Joinpoint point) {Personimpl P=(Personimpl) point.gettarget (); }         Public voidAfteradvice (Joinpoint point) {Personimpl P=(Personimpl) point.gettarget (); }         Public voidAfterreturningadvice (Object retvalue) {System.out.println (RetValue); }         Public voidAroundadvice (Proceedingjoinpoint point)throwsthrowable{System.out.println ("Aroundadvice >> target objects before");        Point.proceed (Point.getargs ()); System.out.println ("After Aroundadvice >> target Object"); }         Public voidThrowingadvice (Throwable throwing)throwsthrowable{System.out.println (throwing); }}

2. Configuration

<!--Slice Managed Bean -<BeanID= "Myaspect"class= "Com.cjm.aop.MyAspect"/><Aop:configProxy-target-class= "true">    <!--Declare a slice -    <Aop:aspectref= "Myaspect"Order= "0">        <!--declaring a pointcut -        <Aop:pointcutID= "POINTCUT1"expression= "Execution (* COM.CJM.AOP). *(..))"/>                <!--Notification of claims -        <Aop:beforePointcut-ref= "POINTCUT1"Method= "Beforeadvice"/>        <Aop:afterPointcut-ref= "POINTCUT1"Method= "Afteradvice"/>        <aop:after-returningPointcut-ref= "POINTCUT1"Method= "Afterreturningadvice"returning= "RetValue"/>        <Aop:aroundPointcut-ref= "POINTCUT1"Method= "Aroundadvice"/>        <aop:after-throwingPointcut-ref= "POINTCUT1"Method= "Throwingadvice"throwing= "throwing"/>    </Aop:aspect></Aop:config>

Spring AOP uses grooming: automatic proxy and AOP command space

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.