"Spring Combat"--10 AOP notification for parameters

Source: Internet
Author: User

With the previous study, it is possible to understand that spring's AOP can be easily monitored for method-level execution , implementing notification responses for a method.

So what about the parameters of the method?

For example, we have a method, every time we pass in a string, I want to know every time the string passed in is God horse? How does this work?

As an example of action above, a thinker (thinker), each time thinking, passes in a string as the thought content.

We want to get to the content of this thinking every time, to implement a notification. So the reader can directly monitor each incoming content through AOP.

Source Reference

First look at the Thinker's interface and implementation class:

 Package Com.spring.test.aopmind;  Public Interface Thinker {    void  thinkofsomething (String thoughts);}
 Package Com.spring.test.aopmind;  Public class Implements thinker{    private  String thoughts;      Public void thinkofsomething (String thoughts) {        this. Thoughts = thoughts;    }       Public String getthoughts () {        return  thoughts;    }}

Here is the interface and implementation class for the reader:

 package   Com.spring.test.aopmind;  public  interface   MindReader {  Interceptthoughts (String thougths); String getthoughts ();}  
 package   Com.spring.test.aopmind;  public  class  Magician implements   mindreader{ private   String thoughts;  public  void   Interceptthoughts (String thougths) {System.out.println ( "intercepting volunteer ' s Thoughts" );     this . Thoughts = thougths;  public   String Getthoughts () {return   thoughts; }}

Then configure the Bean.xml 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:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring- Aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd " >         <BeanID= "Magician"class= "Com.spring.test.aopmind.Magician"/>    <BeanID= "Xingoo"class= "Com.spring.test.aopmind.Volunteer" />        <Aop:configProxy-target-class= "true">            <Aop:aspectref= "Magician">            <Aop:pointcutID= "thinking"expression= "Execution (* com.spring.test.aopmind.Thinker.thinkOfSomething (String)) and args (thoughts)"/>            <Aop:beforePointcut-ref= "thinking"Method= "Interceptthoughts"Arg-names= "Thoughts"/>        </Aop:aspect>    </Aop:config></Beans>

The test class is as follows

 PackageCom.spring.test.aopmind;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTest { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Bean.xml"); Thinker Thinker= (thinker) Ctx.getbean ("Xingoo"); Thinker.thinkofsomething ("Eat something!" "); }}

Execution Result:

Intercepting volunteer ' s thoughts

Explanation of explanation

In the configuration file:

The parameters to be passed in <aop:before> are indicated thoughts

Locking to specific methods and parameters in <aop:pointcut> pointcuts through ASPECTJ expressions Thoughts

Thus, when executing to Method thinkofsomething (), AOP is triggered, the parameter is thoughts, and the interception method is passed to the notification class.

"Spring Combat"--10 AOP notification for parameters

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.