spring aop入门1

来源:互联网 发布:原生js特效懒人之家 编辑:程序博客网 时间:2024/06/08 12:35
package com.aop.inter;public interface Waiter {public void greetTo(String name);public void serveTo(String name);}


package com.aop.impl;import com.aop.inter.Waiter;public class NativeWaiter implements Waiter{public void greetTo(String name) {// TODO Auto-generated method stubSystem.out.println("greet To "+name);}public void serveTo(String name) {// TODO Auto-generated method stubSystem.out.println("serving To "+name);}}

package com.aop.advice;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;/** * 前置增强 * @author sjakl * */public class GreetingBeforeAdvice implements MethodBeforeAdvice {public void before(Method method, Object[] args, Object target)throws Throwable {// TODO Auto-generated method stubString cilentName = (String)args[0];System.out.println("how are you! "+cilentName);}}

package com.aop.test;import org.junit.Test;import org.springframework.aop.BeforeAdvice;import org.springframework.aop.framework.ProxyFactory;import com.aop.advice.GreetingBeforeAdvice;import com.aop.impl.NativeWaiter;import com.aop.inter.Waiter;public class TestBeforeAdvice {@Testpublic void test1(){Waiter target = new NativeWaiter();BeforeAdvice advice = new GreetingBeforeAdvice();//1、spring提供的代理工厂ProxyFactory pf = new ProxyFactory();//2、设置代理目标pf.setTarget(target);//3、为代理目标添加增加pf.addAdvice(advice);//4、生成代理实例Waiter proxy = (Waiter)pf.getProxy();proxy.greetTo("John");proxy.serveTo("Jom");}}



基于spring的

<?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"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  "><bean id="greetingAdvice" class="com.aop.advice.GreetingBeforeAdvice"></bean><bean id="target" class="com.aop.impl.NativeWaiter"></bean><bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="proxyInterfaces"><value>com.aop.inter.Waiter</value></property><property name="interceptorNames"><list><value>greetingAdvice</value></list></property><property name="target"><ref bean="target" /></property></bean></beans>


后置增强

package com.aop.advice;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;/** * 后置增强 * @author sjakl * */public class GreetingAfterAdvice implements AfterReturningAdvice {public void afterReturning(Object returnValue, Method method,Object[] args, Object target) throws Throwable {// TODO Auto-generated method stub        System.out.println("please enjoy");}}

package com.aop.test;import org.junit.Test;import org.springframework.aop.AfterAdvice;import org.springframework.aop.BeforeAdvice;import org.springframework.aop.framework.ProxyFactory;import com.aop.advice.GreetingAfterAdvice;import com.aop.advice.GreetingBeforeAdvice;import com.aop.impl.NativeWaiter;import com.aop.inter.Waiter;public class TestBeforeAdvice {@Testpublic void test1(){Waiter target = new NativeWaiter();BeforeAdvice advice = new GreetingBeforeAdvice();AfterAdvice  afteradvice = new GreetingAfterAdvice();//1、spring提供的代理工厂ProxyFactory pf = new ProxyFactory();//2、设置代理目标pf.setTarget(target);//3、为代理目标添加增加pf.addAdvice(advice);pf.addAdvice(afteradvice);//4、生成代理实例Waiter proxy = (Waiter)pf.getProxy();proxy.greetTo("John");proxy.serveTo("Jom");}}


0 0
原创粉丝点击