Spring AOP Pre-enhancement, post-enhancement small demo

Source: Internet
Author: User
Tags throwable


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+ "...");}}

predecessor Enhancement class Greetingbeforeadvice.java executes before the target class method executes

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, object[] args, Object obj) throws Throwable {string clientName = (string) args[0] ; System.out.println ("How is you! Mr. "+clientname+". ");}

post-Enhancement classGreetingafteradvice.javaexecutes after the target class method call

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.prin TLN ("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 ();//Spring provides proxy factory proxyfactory PF = new Proxyfactory ();//Set proxy target Pf.set Target (target);//Add Enhanced Pf.addadvice (Beforeadvice);p F.addadvice (Afteradvice) for the proxy target;//Generate proxy instance Waiter proxy = (waiter) Pf.getproxy ();p Roxy.greetto ("John");p Roxy.serveto ("Tomcat");}

Output results

How is you! Mr.John.greet to John ... Please enjoy yourself! How is you! Mr.Tomcat.serving Tomcat ... Please enjoy yourself!

configuring with a configuration file Beans.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "><bean id=" Greetingbefore "class=" Com.paic.zhangqi.spring.aop.GreetingBeforeAdvice "/ ><bean id= "Greetingafter" class= "Com.paic.zhangqi.spring.aop.GreetingAfterAdvice"/><bean id= "target" class= "Com.paic.zhangqi.spring.aop.NaiveWaiter"/><bean id= "Waiter" class= " Org.springframework.aop.framework.ProxyFactoryBean "><property name=" proxyinterfaces "value=" Com.paic.zhangqi.spring.aop.Waiter "/><property name=" Interceptornames "value=" Greetingbefore,greetingafter "/><property name=" target "ref=" target "/></bean></beans>

the corresponding test classSpringconfigtest.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");}}

Also get output

How is you! Mr.John.greet to John ... Please enjoy yourself! How is you! Mr.Tomcat.serving Tomcat ... Please enjoy yourself!


Spring AOP Pre-enhancement, post-enhancement small demo

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.