Title: There is a mind reader who needs to do two things: intercept the hearts of the volunteers and show what they're thinking.
1<?xml version= "1.0" encoding= "UTF-8"?>2 3<beans xmlns= "Http://www.springframework.org/schema/beans"4Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"6xmlns:tx= "Http://www.springframework.org/schema/tx"7xmlns:p= "http://www.springframework.org/schema/p"8xsi:schemalocation="9http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdTenhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd Onehttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> A - -<bean id= "Magician"class= "Imple." Magician "/> the -<aop:config> -<aop:aspect ref= "Magician" > -<aop:pointcut expression= "Execution (* Inter. Thinker.thinkofsomething (String)) and args (thoughts)"Id=" thingking "/> +<aop:before method= "interceptthoughts" pointcut-ref= "thingking"arg-names= "Thoughts"/> -</aop:aspect> +</aop:config> -</beans>
Reading minds:
1 Package Inter; 2 3 Public Interface MindReader {4 5 void Interceptthoughts (String thoughts); 6 7 String getthoughts (); 8 9 }
1 Packageimple;2 3 ImportInter. MindReader;4 5 Public classMagicianImplementsmindreader{6 7 PrivateString Thoughts;8 9 Public voidInterceptthoughts (String thoughts) {Ten OneSystem.out.println ("Front intercepting Volunter ' s Thoughts"); A This. Thoughts =thoughts; - System.out.println (thoughts); -SYSTEM.OUT.PRINTLN ("Rear"); the } - - PublicString getthoughts () { - returnthoughts; + } - +}
Volunteers:
1 Package Inter; 2 3 Public Interface Thinker {4 void thinkofsomething (String thoughts); 5 }
1 Packageimple;2 3 ImportInter. Thinker;4 5 Public classVolunteerImplementsthinker{6 7 PrivateString Thoughts;8 9 Public voidthinkofsomething (String thoughts) {Ten This. Thoughts =thoughts; One } A - PublicString getthoughts () { - returnthoughts; the } -}
Test code:
1 @Test 2 Public void test7 () {3 Magician Magician = (Magician) ctx.getbean ("Magician"); 4 Magician.interceptthoughts ("SSSs"); 5 }
Operation Result:
Front intercepting Volunter ' s thoughtsssss rear
Spring with parameter Wrapping notification Demo