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