spring含参数 环绕通知demo

时间:2024-03-25 15:07:14

题目:有一个懂得读心术的人需要完成两件事情:截听志愿者的内心感应和显示他们在想什么

 <?xml version="1.0" encoding="UTF-8"?>

 <beans xmlns="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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="magician" class="imple.Magician"/> <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>

读心术:

 package inter;

 public interface MindReader {

     void interceptThoughts(String thoughts);

     String getThoughts();

 }
 package imple;

 import inter.MindReader;

 public class Magician implements MindReader{

     private String thoughts;

     public void interceptThoughts(String thoughts) {

         System.out.println("前置Intercepting volunter's thoughts");
this.thoughts = thoughts;
System.out.println(thoughts);
System.out.println("后置");
} public String getThoughts() {
return thoughts;
} }

志愿者:

 package inter;

 public interface Thinker {
void thinkOfSomething(String thoughts);
}
 package imple;

 import inter.Thinker;

 public class Volunteer implements Thinker{

     private String thoughts;

     public void thinkOfSomething(String thoughts) {
this.thoughts = thoughts;
} public String getThoughts(){
return thoughts;
}
}

测试代码:

     @Test
public void test7(){
Magician magician = (Magician) ctx.getBean("magician");
magician.interceptThoughts("ssss");
}

运行结果:

前置Intercepting volunter's thoughts
ssss
后置

----------------------------------------------------------基于注解前置通知---------------------------------------------------------------------------------

 <?xml version="1.0" encoding="UTF-8"?>

 <beans xmlns="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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy><!-- 自定义配置文件 --> <bean id="magicianAspectJ" class="imple.MagicianAspectJ"/> </beans>
 package imple;

 import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; import inter.MindReader;

/**
* 读心者读取志愿者信息
*/

 @Aspect
public class MagicianAspectJ implements MindReader{ private String thoughts; @Pointcut("execution(* inter.Thinker.thinkOfSomething(String)) && args(thoughts)")
public void thinking(String thoughts){ } @Before("thinking(thoughts)")
public void interceptThoughts(String thoughts) {
System.out.println("Intercepting volunteer's thoughts: " + thoughts);
this.thoughts = thoughts;
} public String getThoughts() {
return thoughts;
} }
     @Test
public void test11(){
MagicianAspectJ magician = (MagicianAspectJ) ctx.getBean("magicianAspectJ");
magician.interceptThoughts("sdfsdf");
}

结果

Intercepting volunteer's thoughts: sdfsdf