zoukankan      html  css  js  c++  java
  • SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-008-带参数的ADVICE

    一、

    假设有情形如:cd里有很多轨,当播放音乐时,要统计每个音轨的播放次数,这些统计操作不应放在播放方法里,因为统计不是播放音乐的主要职责,这种情况适合应用AOP。

    二、

    1.

    package soundsystem;
    
    public interface CompactDisc {
    
      void play();
      void playTrack(int trackNumber);
    }

    2.

     1 package soundsystem;
     2 
     3 import java.util.List;
     4 
     5 public class BlankDisc implements CompactDisc {
     6 
     7     private String title;
     8     private String artist;
     9     private List<String> tracks;
    10 
    11     public BlankDisc() {
    12         super();
    13     }
    14 
    15     public BlankDisc(String title, String artist, List<String> tracks) {
    16         this.title = title;
    17         this.artist = artist;
    18         this.tracks = tracks;
    19     }
    20 
    21     public void play() {
    22         System.out.println("Playing " + title + " by " + artist);
    23         for (String track : tracks) {
    24             System.out.println("-Track: " + track);
    25         }
    26     }
    27 
    28     public String getTitle() {
    29         return title;
    30     }
    31 
    32     public void setTitle(String title) {
    33         this.title = title;
    34     }
    35 
    36     public String getArtist() {
    37         return artist;
    38     }
    39 
    40     public void setArtist(String artist) {
    41         this.artist = artist;
    42     }
    43 
    44     public List<String> getTracks() {
    45         return tracks;
    46     }
    47 
    48     public void setTracks(List<String> tracks) {
    49         this.tracks = tracks;
    50     }
    51 
    52     @Override
    53     public void playTrack(int trackNumber) {
    54         System.out.println("playTrack---->" + trackNumber);
    55     }
    56 
    57 }

    3.

     1 package soundsystem;
     2 
     3 import java.util.HashMap;
     4 import java.util.Map;
     5 import org.aspectj.lang.annotation.Aspect;
     6 import org.aspectj.lang.annotation.Before;
     7 import org.aspectj.lang.annotation.Pointcut;
     8 
     9 @Aspect
    10 public class TrackCounter {
    11     private Map<Integer, Integer> trackCounts = new HashMap<Integer, Integer>();
    12 
    13     @Pointcut("execution(* soundsystem.CompactDisc.playTrack(int)) "
    14             + "&& args(trackNumber)")
    15     public void trackPlayed(int trackNumber) {
    16     }
    17 
    18     @Before("trackPlayed(trackNumber)")
    19     public void countTrack(int trackNumber) {
    20         int currentCount = getPlayCount(trackNumber);
    21         trackCounts.put(trackNumber, currentCount + 1);
    22     }
    23 
    24     public int getPlayCount(int trackNumber) {
    25         return trackCounts.containsKey(trackNumber) ? trackCounts
    26                 .get(trackNumber) : 0;
    27     }
    28 }

    The thing to focus on in the figure is the args(trackNumber) qualifier in the pointcut expression. This indicates that any int argument that is passed into the execution of playTrack() should also be passed into the advice. The parameter name,trackNumber , also matches the parameter in the pointcut method signature.

    4.配置文件

     1 package soundsystem;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import org.springframework.context.annotation.Bean;
     7 import org.springframework.context.annotation.Configuration;
     8 import org.springframework.context.annotation.EnableAspectJAutoProxy;
     9 
    10 @Configuration
    11 @EnableAspectJAutoProxy
    12 public class TrackCounterConfig {
    13     @Bean
    14     public CompactDisc cd() {
    15         BlankDisc cd = new BlankDisc();
    16         cd.setTitle("Sgt. Pepper's Lonely Hearts Club Band");
    17         cd.setArtist("The Beatles");
    18         List<String> tracks = new ArrayList<String>();
    19         tracks.add("Sgt. Pepper's Lonely Hearts Club Band");
    20         tracks.add("With a Little Help from My Friends");
    21         tracks.add("Lucy in the Sky with Diamonds");
    22         tracks.add("Getting Better");
    23         tracks.add("Fixing a Hole");
    24         // ...other tracks omitted for brevity...
    25         cd.setTracks(tracks);
    26         return cd;
    27     }
    28 
    29     @Bean
    30     public TrackCounter trackCounter() {
    31         return new TrackCounter();
    32     }
    33 }

    5.或xml作配置文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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" xsi:schemaLocation="http://www.springframework.org/schema/aop
     3 http://www.springframework.org/schema/aop/spring-aop.xsd
     4 http://www.springframework.org/schema/beans
     5 http://www.springframework.org/schema/beans/spring-beans.xsd">
     6     <bean id="trackCounter" class="soundsystem.TrackCounter" />
     7     <bean id="cd" class="soundsystem.BlankDisc">
     8         <property name="title" value="Sgt. Pepper's Lonely Hearts Club Band" />
     9         <property name="artist" value="The Beatles" />
    10         <property name="tracks">
    11             <list>
    12                 <value>Sgt. Pepper's Lonely Hearts Club Band</value>
    13                 <value>With a Little Help from My Friends</value>
    14                 <value>Lucy in the Sky with Diamonds</value>
    15                 <value>Getting Better</value>
    16                 <value>Fixing a Hole</value>
    17                 <!-- ...other tracks omitted for brevity... -->
    18             </list>
    19         </property>
    20     </bean>
    21     <aop:config>
    22         <aop:aspect ref="trackCounter">
    23             <aop:pointcut id="trackPlayed" expression="execution(* soundsystem.CompactDisc.playTrack(int))
    24 and args(trackNumber)" />
    25             <aop:before pointcut-ref="trackPlayed" method="countTrack" />
    26         </aop:aspect>
    27     </aop:config>
    28 </beans>

    6.测试

     1 package soundsystem;
     2 
     3 import static org.junit.Assert.assertEquals;
     4 
     5 import org.junit.Rule;
     6 import org.junit.Test;
     7 import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
     8 import org.junit.runner.RunWith;
     9 import org.springframework.beans.factory.annotation.Autowired;
    10 import org.springframework.test.context.ContextConfiguration;
    11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    12 
    13 @RunWith(SpringJUnit4ClassRunner.class)
    14 @ContextConfiguration(classes = TrackCounterConfig.class)
    15 public class TrackCounterTest {
    16     @Rule
    17     public final StandardOutputStreamLog log = new StandardOutputStreamLog();
    18     @Autowired
    19     private CompactDisc cd;
    20     @Autowired
    21     private TrackCounter counter;
    22 
    23     @Test
    24     public void testTrackCounter() {
    25         cd.playTrack(1);
    26         cd.playTrack(2);
    27         cd.playTrack(3);
    28         cd.playTrack(3);
    29         cd.playTrack(3);
    30         cd.playTrack(3);
    31         cd.playTrack(7);
    32         cd.playTrack(7);
    33         assertEquals(1, counter.getPlayCount(1));
    34         assertEquals(1, counter.getPlayCount(2));
    35         assertEquals(4, counter.getPlayCount(3));
    36         assertEquals(0, counter.getPlayCount(4));
    37         assertEquals(0, counter.getPlayCount(5));
    38         assertEquals(0, counter.getPlayCount(6));
    39         assertEquals(2, counter.getPlayCount(7));
    40     }
    41 }
  • 相关阅读:
    JavaScript 复制变量的三种方法
    WIndows cmd command 指令总结
    Linux 指令总结
    如何在当前目录下打开Windows cmd?
    git使用笔记(四)错误报告 Git push rejected error: fatal: refusing to merge unrelated histories
    CentOS(Linux)
    CentOS(Linux)
    异常描述:hibernate懒加载中,用OpenSessionInViewFilter解决之后,同时对一个collection创建两个session访问导致异常(Illegal attempt to associate a collection with two open sessions)
    【转】hibernate懒加载的问题,failed to lazily initialize a collection of role
    JavaWeb项目里面的路径获取方法总结
  • 原文地址:https://www.cnblogs.com/shamgod/p/5239999.html
Copyright © 2011-2022 走看看