1 public interface Waiter { 2 void greetTo(String name); 3 4 void serveTo(String name); 5 }
1 package com.asm; 2 3 public class NaiveWaiter implements Waiter { 4 5 public void greetTo(String name) { 6 System.out.println("greet to " + name + "..."); 7 } 8 9 public void serveTo(String name) { 10 System.out.println("serving " + name + "..."); 11 } 12 }
1 package com.asm; 2 3 public class AdviceMethods { 4 5 public void preGreeting(){ 6 System.out.println("hello====word"); 7 } 8 }
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 <aop:config proxy-target-class="true"> 16 <aop:aspect ref="adviceMethods"> 17 <aop:before pointcut="target(com.asm.NaiveWaiter) and execution (* greetTo(..))" method="preGreeting"/> 18 </aop:aspect> 19 </aop:config> 20 21 <bean id="adviceMethods" class="com.asm.AdviceMethods"></bean> 22 <bean id="navieWaiter" class="com.asm.NaiveWaiter"></bean> 23 </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("navieWaiter"); 13 waiter.greetTo("join"); 14 } 15 }
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 <aop:config proxy-target-class="true"> 16 <aop:aspect ref="adviceMethods"> 17 <aop:pointcut expression="target(com.asm.NaiveWaiter) and execution (* greetTo(..)) " id="greetToPointcut"/> 18 <aop:before method="preGreeting" pointcut-ref="greetToPointcut"/> 19 </aop:aspect> 20 </aop:config> 21 22 <bean id="adviceMethods" class="com.asm.AdviceMethods"></bean> 23 <bean id="navieWaiter" class="com.asm.NaiveWaiter"></bean> 24 </beans>