麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

《Spring實戰》學習筆記(三)面向切面的Spring

2019-11-14 09:13:39
字體:
來源:轉載
供稿:網友

使用注解創建切面

定義切面

代碼結構

這里寫圖片描述

程序清單

PS:定義切面

package concert;import org.aspectj.lang.annotation.*;@Aspectpublic class Audience { @Pointcut("execution(** concert.Performance.perform(..))") public void performance() {} @Before("performance()") public void silenceCellPhones() { System.out.PS:java配置,@EnableAspectJAutoProxy啟用AspectJ自動代理

package concert;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration@EnableAspectJAutoProxy@ComponentScanpublic class ConcertConfig { @Bean public Audience audience() { return new Audience(); }}

PS:切點接口

package concert;public interface Performance { public void perform();}

PS:切點接口實現

package concert;import org.springframework.stereotype.Component;@Componentpublic class PerformanceImpl implements Performance { public void perform() { System.out.println("Singing!!!"); }}

PS:如果通過xml裝配bean

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="concert"/> <aop:aspectj-autoproxy /> <bean class="concert.Audience" /></beans>

測試

import concert.ConcertConfig;import concert.Performance;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;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = ConcertConfig.class)public class PerformanceTest { @Autowired private Performance performance; @Test public void perform() { performance.perform(); }}

這里寫圖片描述

創建環繞通知

PS:把上面的切面代碼改為如下。

package concert;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;@Aspectpublic class Audience { @Pointcut("execution(** concert.Performance.perform(..))") public void performance() {} @Around("performance()") public void watchPerformance(ProceedingJoinPoint jp) { try { System.out.println("Silencing cell phones"); System.out.println("Taking seats"); jp.proceed(); System.out.println("CLAP CLAP CLAP!!!"); } catch (Throwable e) { System.out.println("Demanding a refund"); } }}

處理通知中的參數

代碼結構

這里寫圖片描述

程序清單

package soundsystem;import java.util.List;public class BlankDisc implements CompactDisc { private String title; private String artist; private List<String> tracks; public void setTitle(String title) { this.title = title; } public void setArtist(String artist) { this.artist = artist; } public void setTracks(List<String> tracks) { this.tracks = tracks; } public void play() { System.out.println("Playing "+title+" by "+artist); for(String track : tracks) { System.out.println("-Track: "+track); } } public void playTrack(int trackNumber) { System.out.println("Playing track"+trackNumber); }}package soundsystem;public interface CompactDisc { public void play(); public void playTrack(int trackNumber);}package soundsystem;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import java.util.HashMap;import java.util.Map;@Aspectpublic class TrackCounter { private Map<Integer, Integer> trackCounts = new HashMap<Integer, Integer>(); @Pointcut("execution(* soundsystem.CompactDisc.playTrack(int)) "+"&& args(trackNumber)") public void trackPlayed(int trackNumber) {} @Before("trackPlayed(trackNumber)") public void countTrack(int trackNumber) { int currentCount = getPlayCount(trackNumber); trackCounts.put(trackNumber, currentCount+1); } public int getPlayCount(int trackNumber) { return trackCounts.containsKey(trackNumber) ? trackCounts.get(trackNumber) : 0; }}package soundsystem;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;import java.util.ArrayList;import java.util.List;@Configuration@EnableAspectJAutoProxypublic class TrackCounterConfig { @Bean public CompactDisc sgtPeppers() { BlankDisc cd = new BlankDisc(); cd.setTitle("hello"); cd.setArtist("world"); List<String> tracks = new ArrayList<String>(); tracks.add("11111"); tracks.add("22222"); tracks.add("33333"); tracks.add("44444"); tracks.add("55555"); cd.setTracks(tracks); return cd; } @Bean public TrackCounter trackCounter() { return new TrackCounter(); }}

測試

import org.junit.Rule;import org.junit.Test;import org.junit.contrib.java.lang.system.StandardOutputStreamLog;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;import soundsystem.CompactDisc;import soundsystem.TrackCounter;import soundsystem.TrackCounterConfig;import static org.junit.Assert.assertEquals;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes= TrackCounterConfig.class)public class TrackCounterTest { @Rule public final StandardOutputStreamLog log = new StandardOutputStreamLog(); @Autowired private CompactDisc cd; @Autowired private TrackCounter counter; @Test public void testTrackCounter() { cd.playTrack(1); cd.playTrack(2); cd.playTrack(3); cd.playTrack(3); cd.playTrack(3); cd.playTrack(3); cd.playTrack(7); cd.playTrack(7); assertEquals(1, counter.getPlayCount(1)); assertEquals(1, counter.getPlayCount(2)); assertEquals(4, counter.getPlayCount(3)); assertEquals(0, counter.getPlayCount(4)); assertEquals(0, counter.getPlayCount(5)); assertEquals(0, counter.getPlayCount(6)); assertEquals(2, counter.getPlayCount(7)); }}

這里寫圖片描述

通過注解引入新功能

參考:Spring Aop 使用注解引入新功能

代碼結構

這里寫圖片描述

程序清單

PS:不需要,沒有用到

package concert;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;@Aspectpublic class Audience { @Pointcut("execution(** concert.Performance.perform(..))") public void performance() {} @Around("performance()") public void watchPerformance(ProceedingJoinPoint jp) { try { System.out.println("Silencing cell phones"); System.out.println("Taking seats"); jp.proceed(); System.out.println("CLAP CLAP CLAP!!!"); } catch (Throwable e) { System.out.println("Demanding a refund"); } }}

PS:注入EncoreableIntroducer

package concert;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration@EnableAspectJAutoProxy@ComponentScanpublic class ConcertConfig { @Bean public Audience audience() { return new Audience(); } @Bean public EncoreableIntroducer encoreableIntroducer() { return new EncoreableIntroducer(); }}

PS:Encoreable實現類

package concert;public class DefaultEncoreable implements Encoreable { public void performEncore() { System.out.println("performEncore!!!"); }}

PS:Encoreable接口

package concert;public interface Encoreable { public void performEncore();}

PS:EncoreableIntroducer切面,加號表示Performance所有子類型,不是Performance本身

package concert;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.DeclareParents;@Aspectpublic class EncoreableIntroducer { @DeclareParents(value="concert.Performance+",defaultImpl = DefaultEncoreable.class) public static Encoreable encoreable;}

PS:Performance接口

package concert;public interface Performance { public void perform();}

PS:Performance實現類

package concert;import org.springframework.stereotype.Component;@Componentpublic class PerformanceImpl implements Performance { public void perform() { System.out.println("Singing!!!"); }}

測試

import concert.ConcertConfig;import concert.Encoreable;import concert.Performance;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.applicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = ConcertConfig.class)public class PerformanceTest { @Autowired private ApplicationContext context; @Test public void performEncore() { //對象performanceImpl不僅有PerformanceImpl的功能,也有DefaultEncoreable類的功能 Encoreable encoreable = (Encoreable) context.getBean("performanceImpl"); encoreable.performEncore(); }}

這里寫圖片描述


在XML聲明切面

聲明前置和后置通知

<aop:config> <aop:aspect ref="audience"> <aop:pointcut id="performance" expression="execution(** concert.Performance.perform(..))"/> <aop:before pointcut-ref="performance" method="silenceCellPhones" /> <aop:before pointcut-ref="performance" method="takeSeats" /> <aop:after-returning pointcut-ref="performance" method="applause" /> <aop:after-throwing pointcut-ref="performance" method="demandRefund" /> </aop:aspect></aop:config>package concert;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;public class Audience { public void silenceCellPhones() { System.out.println("Silencing cell phones"); } public void takeSeats() { System.out.println("Taking seats"); } public void applause() { System.out.println("CLAP CLAP CLAP!!!"); } public void demandRefund() { System.out.println("Demanding a refund"); }}

聲明環繞通知

<aop:config> <aop:aspect ref="audience"> <aop:pointcut id="performance" expression="execution(** concert.Performance.perform(..))"/> <aop:around pointcut-ref="performance" method="watchPerformance"/> </aop:aspect></aop:config>package concert;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;public class Audience { public void watchPerformance(ProceedingJoinPoint jp) { try { System.out.println("Silencing cell phones"); System.out.println("Taking seats"); jp.proceed(); System.out.println("CLAP CLAP CLAP!!!"); } catch (Throwable e) { System.out.println("Demanding a refund"); } }}

為通知傳遞參數

package soundsystem;import java.util.HashMap;import java.util.Map;public class TrackCounter { private Map<Integer, Integer> trackCounts = new HashMap<Integer, Integer>(); public void countTrack(int trackNumber) { int currentCount = getPlayCount(trackNumber); trackCounts.put(trackNumber, currentCount+1); } public int getPlayCount(int trackNumber) { return trackCounts.containsKey(trackNumber) ? trackCounts.get(trackNumber) : 0; }}<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="trackCounter" class="soundsystem.TrackCounter"/> <bean id="cd" class="soundsystem.BlankDisc"> <property name="title" value="hello"/> <property name="artist" value="world"/> <property name="tracks"> <list> <value>11111</value> <value>22222</value> <value>33333</value> <value>44444</value> <value>55555</value> </list> </property> </bean> <aop:config> <aop:aspect ref="trackCounter"> <aop:pointcut id="trackPlayed" expression="execution(* soundsystem.CompactDisc.playTrack(int)) and args(trackNumber)"/> <aop:before pointcut-ref="trackPlayed" method="countTrack"/> </aop:aspect> </aop:config></beans>

通過切面引入新的功能

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="concert"/> <aop:aspectj-autoproxy /> <bean id="audience" class="concert.Audience" /> <bean id="encorebleDelegate" class="concert.DefaultEncoreable"/> <aop:config> <aop:aspect ref="audience"> <aop:pointcut id="performance" expression="execution(** concert.Performance.perform(..))"/> <aop:around pointcut-ref="performance" method="watchPerformance"/> </aop:aspect> <aop:aspect> <aop:declare-parents types-matching="concert.Performance+" implement-interface="concert.Encoreable" delegate-ref="encorebleDelegate"/> </aop:aspect> </aop:config></beans>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美999| 亚洲最大中文字幕 | 国产在线精品一区二区三区 | 久久久免费 | 久久久久国产成人免费精品免费 | 99re热视频这里只精品 | 一级国产精品一级国产精品片 | 黄色免费在线视频网站 | 成人国产精品一区二区毛片在线 | 成人在线视频在线观看 | 成人一级黄色 | 欧美成人综合视频 | 成人一区二区三区在线 | 亚洲一区二区免费 | 激情在线视频 | jizzjizzjizz少妇 | 中文字幕免费一区 | 一级大片久久 | 99精彩视频在线观看 | 黄色av网站免费 | 久久一本日日摸夜夜添 | 国产91一区 | 羞羞羞网站| 一区二区久久久久草草 | 欧美高清第一页 | 亚洲欧美一区二区三区在线观看 | 久久久婷婷一区二区三区不卡 | 久久久久久久91 | 国产又粗又爽又深的免费视频 | 黄在线观看在线播放720p | arabxxxxvideos| 色网站在线免费观看 | 国产精品爱久久久久久久 | 久草在线小说 | 色播视频网站 | 日日鲁夜夜视频热线播放 | 中文黄色一级片 | 国产91九色 | 午夜视频福利 | 老司机免费福利午夜入口ae58 | 人人看人人舔 |