国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

《Spring實(shí)戰(zhàn)》學(xué)習(xí)筆記(三)面向切面的Spring

2019-11-14 09:53:11
字體:
供稿:網(wǎng)友

使用注解創(chuàng)建切面

定義切面

代碼結(jié)構(gòu)

這里寫圖片描述

程序清單

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自動(dòng)代理

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:切點(diǎn)接口

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

PS:切點(diǎn)接口實(shí)現(xiàn)

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(); }}

這里寫圖片描述

創(chuàng)建環(huán)繞通知

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"); } }}

處理通知中的參數(shù)

代碼結(jié)構(gòu)

這里寫圖片描述

程序清單

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 使用注解引入新功能

代碼結(jié)構(gòu)

這里寫圖片描述

程序清單

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實(shí)現(xiàn)類

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切面,加號(hào)表示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實(shí)現(xiàn)類

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() { //對(duì)象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"); }}

聲明環(huán)繞通知

<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"); } }}

為通知傳遞參數(shù)

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>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 抚松县| 马山县| 盱眙县| 牙克石市| 茂名市| 泰兴市| 乐清市| 明水县| 大方县| 兰西县| 贺兰县| 冀州市| 河西区| 包头市| 临沧市| 西华县| 安徽省| 徐水县| 福海县| 长泰县| 礼泉县| 土默特左旗| 保山市| 鸡泽县| 南城县| 夏邑县| 潢川县| 五台县| 临沭县| 金坛市| 综艺| 余庆县| 崇阳县| 大同市| 康乐县| 四平市| 黄石市| 兴隆县| 新营市| 同心县| 淳化县|