1 public interface Waiter { 2 void greetTo(String name); 3 4 void serveTo(String name); 5 }
1 public class NaiveWaiter implements Waiter { 2 3 public void greetTo(String name) { 4 System.out.println("greet to " + name + "..."); 5 } 6 7 public void serveTo(String name) { 8 System.out.println("serving " + name + "..."); 9 } 10 }
1 @Aspect 2 public class PreGreetingAspect { 3 4 @Before("execution(* greetTo(..))") 5 public void beforeGreeting(){ 6 7 System.out.println("how are you"); 8 } 9 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 <!-- 扫描类包下的类 这样Spring定义的注解才能产生作用 如@Resposity @Autowired --> 16 <bean id="waiter" class="com.asm.NaiveWaiter"></bean> 17 <bean class="com.asm.PreGreetingAspect"></bean> 18 19 <bean 20 class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> 21 </beans>
1 package com.asm; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 import org.testng.annotations.Test; 6 7 public class SpringTest1 { 8 9 public static void main(String[] args) { 10 11 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 12 Waiter waiter = (Waiter)ctx.getBean("waiter"); 13 waiter.greetTo("join"); 14 waiter.serveTo("join"); 15 } 16 }
基于Schema的配置
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.1.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 14 15 <!-- 扫描类包下的类 这样Spring定义的注解才能产生作用 如@Resposity @Autowired --> 16 <bean id="waiter" class="com.asm.NaiveWaiter"></bean> 17 <bean class="com.asm.PreGreetingAspect"></bean> 18 //基于@Aspectj的切面配置 19 <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 20 </beans>
1 package com.asm; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 import org.testng.annotations.Test; 6 7 public class SpringTest1 { 8 9 public static void main(String[] args) { 10 11 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 12 Waiter waiter = (Waiter)ctx.getBean("waiter"); 13 waiter.greetTo("join"); 14 waiter.serveTo("join"); 15 } 16 }
1 一种方式是使用AspectJ提供的注解: 2 3 package test.mine.spring.bean; 4 5 import org.aspectj.lang.annotation.AfterReturning; 6 import org.aspectj.lang.annotation.Aspect; 7 import org.aspectj.lang.annotation.Before; 8 import org.aspectj.lang.annotation.Pointcut; 9 @Aspect 10 public class SleepHelper { 11 12 public SleepHelper(){ 13 14 } 15 16 @Pointcut("execution(* *.sleep())") 17 public void sleeppoint(){} 18 19 @Before("sleeppoint()") 20 public void beforeSleep(){ 21 System.out.println("睡觉前要脱衣服!"); 22 } 23 24 @AfterReturning("sleeppoint()") 25 public void afterSleep(){ 26 System.out.println("睡醒了要穿衣服!"); 27 } 28 29 } 30 31 用@Aspect的注解来标识切面,注意不要把它漏了,否则Spring创建代理的时候会找不到它,@Pointcut注解指定了切点,@Before和@AfterReturning指定了运行时的通知,注 32 33 意的是要在注解中传入切点的名称 34 35 然后我们在Spring配置文件上下点功夫,首先是增加AOP的XML命名空间和声明相关schema 36 命名空间: 37 xmlns:aop="http://www.springframework.org/schema/aop" 38 schema声明: 39 http://www.springframework.org/schema/aop 40 http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 41 42 然后加上这个标签: 43 <aop:aspectj-autoproxy/> 有了这个Spring就能够自动扫描被@Aspect标注的切面了 44 45 最后是运行,很简单方便了: 46 public class Test { 47 48 public static void main(String[] args){ 49 ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml"); 50 Sleepable human = (Sleepable)appCtx.getBean("human"); 51 human.sleep(); 52 } 53 } 54 55 56 、