Spring AOP 中处理通知中的参数(注解方式)

演员演出需要记录每个演员的演出次数

1. Performance接口和Actor类

package aop;

/**
 * Created by guoli on 2017/7/28 0028.
 * YXV0aG9yOiB6aGFuZ2d1b2xpLmNu
 */
public interface Performance {

    /**
     * @param number 演员编号
     */
    public void playTrack(String number);
}
package aop;

import org.springframework.stereotype.Component;

/**
 * Created by guoli on 2017/7/28 0028.
 * YXV0aG9yOiB6aGFuZ2d1b2xpLmNu
 */
@Component
public class Actor implements Performance {

    @Override
    public void playTrack(String number) {
        System.out.println(number + " number actor");
    }
}

2. 切点表达式匹配演出方法

package aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by guoli on 2017/7/28 0028.
 * YXV0aG9yOiB6aGFuZ2d1b2xpLmNu
 */

/**
 * 注意这里的切点表达式
 * execution(* aop.Performance.playTrack(String)) && args(trackNumber)
 * String :接受String类型的参数
 * trackNumber :指定参数
 *
 */
@Aspect
@Component
public class TrackCountAnnotation {

    //演员编号和演出次数
    private Map<String, Integer> trackCounts = new HashMap<String, Integer>();

    @Pointcut("execution(* aop.Performance.playTrack(String)) && args(trackNumber)")
    public void trackPlayed(String trackNumber) {
    }

    //演出前将该演员的演出次数增加一,不考虑其他情况
    @Before("trackPlayed(trackNumber)")
    public void countTrack(String trackNumber) {
        int currentCount = getPlayCount(trackNumber);
        System.out.println("record " + trackNumber + " people,times " + (currentCount + 1));
        trackCounts.put(trackNumber, ++currentCount);
    }

    private int getPlayCount(String trackNumber) {
        return trackCounts.get(trackNumber) != null ? trackCounts.get(trackNumber) : 0;
    }
}

3. Java注解方式启动自动配置

package aop;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * Created by guoli on 2017/7/28 0028.
 * YXV0aG9yOiB6aGFuZ2d1b2xpLmNu
 */

@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class Config {

}

4. 测试

package aop;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by guoli on 2017/7/28 0028.
 * YXV0aG9yOiB6aGFuZ2d1b2xpLmNu
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class TrackTest {
    @Autowired
    private Performance actor;

    @Test
    public void testNumber(){
        actor.playTrack("Tom");
        actor.playTrack("Anne");
        actor.playTrack("Eva");
        actor.playTrack("Eva");
        actor.playTrack("Eva");
        actor.playTrack("Anne");
        actor.playTrack("Tom");
        actor.playTrack("Tom");
        actor.playTrack("Tom");
    }
}

输出如下:

record Tom people,times 1
Tom number actor

record Anne people,times 1
Anne number actor

record Eva people,times 1
Eva number actor

record Eva people,times 2
Eva number actor

record Eva people,times 3
Eva number actor

record Anne people,times 2
Anne number actor

record Tom people,times 2
Tom number actor

record Tom people,times 3
Tom number actor

record Tom people,times 4
Tom number actor

标签: Spring, advice, aop, annotation, params

相关文章推荐: