spring AOP 前置增强,后置增强小Demo
服务员接口 Waiter.java
package com.paic.zhangqi.spring.aop; public interface Waiter { void greetTo(String name); void serveTo(String name); }
服务员接口实现类 NaiveWaiter.java
package com.paic.zhangqi.spring.aop; public class NaiveWaiter implements Waiter { @Override public void greetTo(String name) { System.out.println("greet to "+name+"..."); } @Override public void serveTo(String name) { System.out.println("serving "+name+"..."); } }
前置增强类
GreetingBeforeAdvice.java 在目标类方法执行前执行
package com.paic.zhangqi.spring.aop; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class GreetingBeforeAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object obj) throws Throwable { String clientName = (String)args[0]; System.out.println("How are you!Mr."+clientName+"."); } }
后置增强类GreetingAfterAdvice.java
在目标类方法调用后执行
package com.paic.zhangqi.spring.aop; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class GreetingAfterAdvice implements AfterReturningAdvice { @Override public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { System.out.println("Please enjoy yourself!"); } }
测试类 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 提供的代理工厂 ProxyFactory pf = new ProxyFactory(); // 设置代理目标 pf.setTarget(target); // 为代理目标添加增强 pf.addAdvice(beforeAdvice); pf.addAdvice(afterAdvice); // 生成代理实例 Waiter proxy = (Waiter)pf.getProxy(); proxy.greetTo("John"); proxy.serveTo("Tomcat"); } }
输出结果
How are you!Mr.John. greet to John... Please enjoy yourself! How are you!Mr.Tomcat. serving Tomcat... Please enjoy yourself!
使用配置文件进行配置 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>
对应的测试类 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"); } }
同样得到输出
How are you!Mr.John. greet to John... Please enjoy yourself! How are you!Mr.Tomcat. serving Tomcat... Please enjoy yourself!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。