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