In Spring AOP, there are 3 common concepts, advices, Pointcut, and Advisor, which are explained as follows:
- Advices: Represents an action before or after a method is executed.
- Pointcut: means to intercept a method based on the name of the method or the regular expression.
- Advisor:advice and Pointcut are composed of separate units, and can be passed to the Proxy factory object.
We can use name matching and regular expression matching to match the method to intercept.
1 Pointcut-name Match Example
Intercept the Printname () method through Pointcut and advisor. Create a namematchmethodpointcut bean and inject the name Printname of the method you want to intercept into the property mappedname, as follows:
<id= "Customerpointcut" class= " Org.springframework.aop.support.NameMatchMethodPointcut "> < Name= "Mappedname" value= "Printname"/></ Bean>
Create a Defaultpointcutadvisor advisor Bean that associates pointcut with advice.
<BeanID= "Customeradvisor"class= "Org.springframework.aop.support.DefaultPointcutAdvisor"> < Propertyname= "Pointcut"ref= "Customerpointcut" /> < Propertyname= "Advice"ref= "Hijackaroundmethodbean" /></Bean>
Change the Interceptornames value of the agent to replace the top advisor (Customeradvisor) with the original Hijackaroundmethodbean.
<BeanID= "Customerserviceproxy"class= "Org.springframework.aop.framework.ProxyFactoryBean"> < Propertyname= "Target"ref= "CustomerService" /> < Propertyname= "Interceptornames"> <List> <value>Customeradvisor</value> </List> </ Property></Bean>
The entire XML is as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <BeanID= "CustomerService"class= "Com.shiyanlou.spring.aop.CustomerService"> < Propertyname= "Name"value= "Shiyanlou" /> < Propertyname= "url"value= "Shiyanlou.com" /> </Bean> <BeanID= "Hijackaroundmethodbean"class= "Com.shiyanlou.spring.aop.HijackAroundMethod" /> <BeanID= "Customerserviceproxy"class= "Org.springframework.aop.framework.ProxyFactoryBean"> < Propertyname= "Target"ref= "CustomerService" /> < Propertyname= "Interceptornames"> <List> <value>Customeradvisor</value> </List> </ Property> </Bean> <BeanID= "Customerpointcut"class= "Org.springframework.aop.support.NameMatchMethodPointcut"> < Propertyname= "Mappedname"value= "Printname" /> </Bean> <BeanID= "Customeradvisor"class= "Org.springframework.aop.support.DefaultPointcutAdvisor"> < Propertyname= "Pointcut"ref= "Customerpointcut" /> < Propertyname= "Advice"ref= "Hijackaroundmethodbean" /> </Bean></Beans>
2 Pointcut-regular exxpression Match Example
You can configure the method with regular expression matching that needs to be intercepted, as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <BeanID= "CustomerService"class= "Com.shiyanlou.spring.aop.advice.CustomerService"> < Propertyname= "Name"value= "Shiyanlou" /> < Propertyname= "url"value= "Shiyanlou.com" /> </Bean> <BeanID= "Hijackaroundmethodbean"class= "Com.shiyanlou.spring.aop.advice.HijackAroundMethod" /> <BeanID= "Customerserviceproxy"class= "Org.springframework.aop.framework.ProxyFactoryBean"> < Propertyname= "Target"ref= "CustomerService" /> < Propertyname= "Interceptornames"> <List> <value>Customeradvisor</value> </List> </ Property> </Bean> <BeanID= "Customeradvisor"class= "Org.springframework.aop.support.RegexpMethodPointcutAdvisor"> < Propertyname= "Patterns"> <List> <value>. *url.*</value> </List> </ Property> < Propertyname= "Advice"ref= "Hijackaroundmethodbean" /> </Bean></Beans>
Spring+maven Learning Experiment-Spring AOP oriented tangent programming (II.)