Java静态动态代理以及spring实现代理

来源:互联网 发布:pythonxy linux 编辑:程序博客网 时间:2024/06/08 15:16

静态代理:



package com.ant.actor;/* * 艺人 */public interface Artist {public void show();}

package com.ant.actor;/* * 代理类 */public class Agency implements Artist{private Artist Artist;public Agency(Artist Artist) {this.Artist = Artist;}public void show() {Artist.show();}}


package com.ant.actor;/* * 演员 */public class Actor implements Artist {public void show() {System.out.println("演员方法调用");}}

动态代理:

代理类需要实现InvocationHandler接口

package com.ant.dynamic;/* * 艺人 */public interface Artist {public void sing();} 


package com.ant.dynamic;/* * 歌手类 */public class ZhangXueYou implements Artist{public void sing() {System.out.println("张学友唱歌");}}

package com.ant.dynamic;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;/* *代理类,实现InvocationHandler接口 */public class ChenShuFen implements InvocationHandler{private Artist Artist;public ChenShuFen(Artist Artist) {this.Artist = Artist;}public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {// TODO Auto-generated method stubObject o=method.invoke(Artist, args);return o;}}

最后贴上测试类

@Test        //动态代理public void AopDynamic() {//com.ant.dynamic.Artist:方法接口;ZhangXueYou:歌手类;ChenShuFen:代理类需要实现InvocationHandler接口com.ant.dynamic.Artist artist=new ZhangXueYou();ChenShuFen shuFen=new ChenShuFen(artist);//混合产生代理对象;Proxy.newProxyInstance(歌手类类加载器(原对象的类加载器),歌手类实现的所有接口(原对象实现的所有接口),代理对象);artist=(com.ant.dynamic.Artist) Proxy.newProxyInstance(ZhangXueYou.class.getClassLoader(), ZhangXueYou.class.getInterfaces(),shuFen );artist.sing();}

spring实现代理


package com.ant.proxy;/* * 艺人 */public interface Artist {public void MultiFunction();}


package com.ant.proxy;/* * 目标 Target */public class HuGe implements Artist{public void MultiFunction() {System.out.println("胡歌是万能型演员");}}

package com.ant.proxy;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;/* *通知 Advince */public class Ben implements MethodInterceptor{public Object invoke(MethodInvocation arg0) throws Throwable {Object o=arg0.proceed();return o;}}

package com.ant.proxy;import org.junit.Test;/* * 测试类 */import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringTest {@Testpublic void Spring() {ApplicationContext con=new ClassPathXmlApplicationContext("applicationcontext.xml");Artist artist=(Artist) con.getBean("myproxy");artist.MultiFunction();}}


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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><!-- 配置目标HuGe --><bean id="huge" class="com.ant.proxy.HuGe"></bean><!-- 配置通知Ben --><bean id="ben" class="com.ant.proxy.Ben"></bean><!-- 配置混合对象 --><bean id="myproxy" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 引用目标 --><property name="target" ref="huge"></property><!-- 目标实现接口,value对应实现接口的全限定名 --><property name="proxyInterfaces"><list><value>com.ant.proxy.Artist</value></list></property><!-- 引用通知 --><property name="interceptorNames" ><list><idref bean="ben"/></list></property></bean></beans>