Spring+maven Learning Experiment-Spring AOP oriented tangent programming (II.)

Source: Internet
Author: User

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.)

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.