Spring AOP pre-enhancement and post-enhancement Demo
Waiter interface Waiter. java
package com.paic.zhangqi.spring.aop;public interface Waiter {void greetTo(String name);void serveTo(String name);}
Waiter interface implementation class NaiveWaiter. java
package com.paic.zhangqi.spring.aop;public class NaiveWaiter implements Waiter {@Overridepublic void greetTo(String name) {System.out.println(greet to +name+...);}@Overridepublic void serveTo(String name) {System.out.println(serving +name+...);}}
The pre-enhancement class GreetingBeforeAdvice. java is executed before the target class method is executed.
package com.paic.zhangqi.spring.aop;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class GreetingBeforeAdvice implements MethodBeforeAdvice {@Overridepublic void before(Method method, Object[] args, Object obj) throws Throwable {String clientName = (String)args[0];System.out.println(How are you!Mr.+clientName+.);}}
The post-enhancement class GreetingAfterAdvice. java is executed after the target class method is called.
package com.paic.zhangqi.spring.aop;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class GreetingAfterAdvice implements AfterReturningAdvice {@Overridepublic void afterReturning(Object arg0, Method arg1, Object[] arg2,Object arg3) throws Throwable {System.out.println(Please enjoy yourself!);}}
Test class TestAdvice. java
Package com. paic. zhangqi. spring. aop; import org. springframework. aop. afterAdvice; import org. springframework. aop. beforeAdvice; import org. springframework. aop. framework. proxyFactory; public class TestAdvice {public static void main (String [] args) {Waiter target = new NaiveWaiter (); BeforeAdvice beforeAdvice = new GreetingBeforeAdvice (); afterAdvice afterAdvice = new GreetingAfterAdvice (); // The proxy factory ProxyFactory pf provided by spring = new ProxyFactory (); // sets the proxy target pf. setTarget (target); // Add an enhanced pf for the proxy target. addAdvice (beforeAdvice); pf. addAdvice (afterAdvice); // generate the proxy instance Waiter proxy = (Waiter) pf. getProxy (); proxy. greetTo (John); proxy. serveTo (Tomcat );}}
Output result
How are you!Mr.John.greet to John...Please enjoy yourself!How are you!Mr.Tomcat.serving Tomcat...Please enjoy yourself!
Use the configuration file to configure beans. xml
Corresponding test class SpringConfigTest. java
package com.paic.zhangqi.spring.aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringConfigTest {public static void main(String[] args) {String configPath = com/paic/zhangqi/spring/aop/beans.xml;ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);Waiter waiter = (Waiter) ctx.getBean(waiter);waiter.greetTo(John);waiter.serveTo(Tomcat);}}
Same output
How are you!Mr.John.greet to John...Please enjoy yourself!How are you!Mr.Tomcat.serving Tomcat...Please enjoy yourself!