一、
情景:有个魔术师会读心术,常人一想一事物他就能读到。以魔术师为切面织入常人的内心。
二、
1.
// <start id="mindreader_java" /> package com.springinaction.springidol; public interface MindReader { void interceptThoughts(String thoughts); String getThoughts(); } // <end id="mindreader_java" />
2.
// <start id="magician_java" /> package com.springinaction.springidol; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class Magician implements MindReader { private String thoughts; @Pointcut("execution(* com.springinaction.springidol." //<co id="co_parameterizedPointcut"/> + "Thinker.thinkOfSomething(String)) && args(thoughts)") public void thinking(String thoughts) { } @Before("thinking(thoughts)") //<co id="co_passInParameters"/> public void interceptThoughts(String thoughts) { System.out.println("Intercepting volunteer's thoughts : " + thoughts); this.thoughts = thoughts; } public String getThoughts() { return thoughts; } } // <end id="magician_java" />
3.
// <start id="thinker_java" /> package com.springinaction.springidol; public interface Thinker { void thinkOfSomething(String thoughts); } // <end id="thinker_java" />
4.
1 // <start id="volunteer_java" /> 2 package com.springinaction.springidol; 3 4 public class Volunteer implements Thinker { 5 private String thoughts; 6 7 public void thinkOfSomething(String thoughts) { 8 this.thoughts = thoughts; 9 } 10 11 public String getThoughts() { 12 return thoughts; 13 } 14 } 15 // <end id="volunteer_java" />
5.sneaky-magician.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop.xsd"> 9 10 <bean id="volunteer" 11 class="com.springinaction.springidol.Volunteer" /> 12 <bean id="magician" 13 class="com.springinaction.springidol.Magician" /> 14 15 <!-- <start id="aspectj_autoproxy" /> --> 16 <aop:aspectj-autoproxy /> 17 <!-- <end id="aspectj_autoproxy" /> --> 18 19 </beans>
6.
1 package com.springinaction.springidol; 2 3 import static org.junit.Assert.*; 4 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.test.context.ContextConfiguration; 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10 11 @RunWith(SpringJUnit4ClassRunner.class) 12 @ContextConfiguration( { "sneaky-magician.xml" }) 13 public class MindReaderTest { 14 @Autowired 15 MindReader magician; 16 17 @Autowired 18 Thinker volunteer; 19 20 // <start id="testMindReader" /> 21 @Test 22 public void magicianShouldReadVolunteersMind() { 23 volunteer.thinkOfSomething("Queen of Hearts"); 24 25 assertEquals("Queen of Hearts", magician.getThoughts()); 26 } 27 // <end id="testMindReader" /> 28 }