Spring AOP pre-enhancement and post-enhancement Demo

Source: Internet
Author: User

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!


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.