详解spring切面使用传递给被通知方法的参数

网友投稿 230 2023-03-09


详解spring切面使用传递给被通知方法的参数

本文介绍了详解spring切面使用传递给被通知方法的参数,分享给大家,具体如下:

场景:

BlankDisc代表CD实体,可以通过playTrack()方法直接播放某一个磁道中的歌曲。

需求是记录每个磁道被播放的次数。

一种方法就是修改playTrack()方法,直接在每次调用的时候记录这个数量。但是,记录磁道的播放次数与播放本身是不同的关注点,因此不应该属于playTrack()方法。这应该是切面要完成的任务。

CompactDisc接口

public interface CompactDisc {

//播放某一个磁道中的歌曲

void playTrack(String track);

}

实现类BlankDisc

public class BlankDisc implements CompactDisc {

private String title;

private String artist;

private List tracks;

public void setTitle(String title) {

this.title = title;

}

public void setArtist(String artist) {

this.artist = artist;

}

public void setTracks(List tracks) {

this.tracks http://= tracks;

}

@Override

public void playTrack(String track) {

System.out.println("-Track: " + track);

}

}

切面类TraceCount

/**

* 切面类的任务:记录每个磁道被播放的次数

* Created by Administrator on 2017/12/1.

*/

@Component

@Aspect

public class TrackCounter {

private Map trackCounts = new HashMap<>();

@Pointcut("execution(* chapter04.aop_args.BlankDisc.playTrack(String)) && args(track)")

public void trackPlayed(String track) {

}

//在播放前,为该磁道计数

@Before("trackPlayed(track)")

public void countTrack(String track) {

int currentCount = getPlayCount(track);

trackCounts.put(track, currentCount + 1);

}

public int getPlayCount(String track) {

return trackCounts.containsKey(track) ? trackCounts.get(track) : 0;

}

}

applicationContext.xml配置文件

class="chapter04.aop_args.BlankDisc">

Sgt. Pepper's Lonely Hearts Club Band

With a LiqrYpcmyttle Help from My Friends

Lucy in the Sky with Diamonds

Getting Better

Fixing a Hole

She's Leaving Home

Being for the Benefit of Mr. Kite!

Within You Without You

When I'm Sixty-Four

Lovely Rita

Good Morning Good Morning

Sgt. Pepper's Lonely Hearts Club Band (Reprise)

A Day in the Life

class="chapter04.aop_args.BlankDisc">

Sgt. Pepper's Lonely Hearts Club Band

With a LiqrYpcmyttle Help from My Friends

Lucy in the Sky with Diamonds

Getting Better

Fixing a Hole

She's Leaving Home

Being for the Benefit of Mr. Kite!

Within You Without You

When I'm Sixty-Four

Lovely Rita

Good Morning Good Morning

Sgt. Pepper's Lonely Hearts Club Band (Reprise)

A Day in the Life

测试

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class test_aop_args {

@Autowired

CompactDisc cd;

@Autowired

TrackCounter trackCounter;

@Test

public void test(){

cd.playTrack("Sgt. Pepper's Lonely Hearts Club Band");

cd.playTrack("With a Little Help from My Friends");

cd.playTrack("Lucy in the Sky with Diamonds");

cd.playTrack("Sgt. Pepper's Lonely Hearts Club Band");

cd.playTrack("With a Little Help from My Friends");

cd.playTrack("Sgt. Pepper's Lonely Hearts Club Band");

System.out.println(trackCounter.getPlayCount("Sgt. Pepper's Lonely Hearts Club Band"));

System.out.println(trackCounter.getPlayCount("With a Little Help from My Friends"));

System.out.println(trackCounter.getPlayCount("Lucy in the Sky whttp://ith Diamonds"));

http:// System.out.println(trackCounter.getPlayCount("Getting Better"));

}

}

测试结果 3,2,1,0

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多qrYpcmy多支持我们。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:包含嵌入式 系统接口设计 linux的词条
下一篇:springboot使用JdbcTemplate完成对数据库的增删改查功能
相关文章

 发表评论

暂时没有评论,来抢沙发吧~