• SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2


    一、

    情景:有个魔术师会读心术,常人一想一事物他就能读到。以魔术师为切面织入常人的内心。

    二、

    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 }
  • 相关阅读:
    跟layout_weight说88,轻松搞定百分比布局
    跟闪退、程序崩溃说88
    程序的需求层次
    开博
    第十章 数组与集合 发牌程序 实例代码
    C#面向对象基础01
    winform form
    html
    C#语言基础02
    C#语言基础01
  • 原文地址:https://www.cnblogs.com/shamgod/p/5240069.html
Copyright © 2020-2023  润新知