标签:.net use exec org ann ice lan print contex
源码下载地址:http://download.csdn.net/download/poiuy1991719/9993015
Spring4 In Action-4.2-@AspectJ-切面
bean接口:
package com.zte.sound.service.bean; /** * 光盘 */ public interface CompactDisc { public void play(); }
Bean接口实现类:
package com.zte.sound.service.bean.imp; import org.springframework.stereotype.Component; import com.zte.sound.service.bean.CompactDisc; //@Component注解会告诉Spring创建这个类的实例bean(注意,启动Component注解功能需要在xml里面配置) //@Component("newName")//这里,配置类已经创建了 public class SgtPeppers implements CompactDisc { private String title; private String artist; public SgtPeppers(String title,String artist){ this.title=title; this.artist=artist; System.out.println("SgtPeppers类实例化"); } public void play() { System.out.println("Sgt Playing:title="+title+" artist="+artist); } }
切面类:
package com.zte.sound.service.bean; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class Audience { //这样就可以在后面调用简化(例如:takeSet()方法) @Pointcut("execution(** com.zte.sound.service.bean.CompactDisc.play(..))") public void perfomanceRef(){} //表演前 @Before("execution(** com.zte.sound.service.bean.CompactDisc.play(..))") public void silenceCellPhones(){ System.out.println("silenceCellPhones:@Before:0:请把手机静音"); } @Before("perfomanceRef()") public void takeSet(){ System.out.println("takeSet:@Before:1:找一个位置坐下"); } //表演后 @AfterReturning("execution(** com.zte.sound.service.bean.CompactDisc.play(..))") public void applause(){ System.out.println("applause:@AfterReturning:鼓掌"); } @AfterThrowing("execution(** com.zte.sound.service.bean.CompactDisc.play(..))") public void demandRefund(){ System.out.println("demandRefund:@AfterThrowing:唱错了重唱"); } }
配置类:
package configs; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.context.annotation.Import; import com.zte.sound.service.bean.Audience; import com.zte.sound.service.bean.CompactDisc; import com.zte.sound.service.bean.imp.SgtPeppers; @Configuration @ComponentScan(basePackageClasses={SgtPeppers.class,Audience.class}) @EnableAspectJAutoProxy //启动AspectJ自动代理 public class CDPlayerConfig { @Bean public CompactDisc returnSgtPeppers(){//播放SgtPeppers return new SgtPeppers("title","artist"); } //申明切入点Audience对象 @Bean public Audience audience(){ return new Audience(); } }
测试类:
package test; 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; import com.zte.sound.service.bean.CompactDisc; import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator; import configs.CDPlayerConfig; @RunWith(SpringJUnit4ClassRunner.class)//Spring的Junit测试,会在测试开始时,创建Spring的应用上下文 @ContextConfiguration(classes=CDPlayerConfig.class)//表明配置类 public class SpringTest1 { //自动装配 @Autowired private CompactDisc sp; @Test public void instanceSpring(){ //自动装配 sp.play(); } }
Spring4 In Action-4.2-@AspectJ-切面
标签:.net use exec org ann ice lan print contex
原文地址:http://www.cnblogs.com/zjsy/p/7587666.html