spring中将静态代理修改为动态代理

上一笔中 如果使用静态代理那么我们的代码会迅速膨胀 ,当然现在接口中的方法还比较少 如果接口中的方法有上百个那是不是完蛋了

我们讲MyProxy静态代理修改为动态代理

/**
* 用于取代YouProxy MyProxy
* @author lenovo
*
*/
public class DynaProxy implements InvocationHandler{

private Cook cook;
private Servant01 s01;
private Servant02 s02;
private Driver driver;
private Object realObj;//代理要依赖的东西 调用真正方法之前 我得调用上面这些对象的一系列方法
//你代理的这个对象 当他调用任何方法的时候其实它都不会去调用那个方法
//而是先调用你这个invoke 你在invoke里面可以做任何你想做的事情
//做完之后你再去掉你正真代理那个对象的(委托类)那个业务方法
//为了动态的创建代理对象
public static Object newInstance(Object realObj,Cook cook,Driver driver,
Servant01 s01,Servant02 s02){
DynaProxy dp = new DynaProxy();
dp.realObj = realObj;
dp.cook = cook;
dp.driver = driver;
dp.s01 = s01;
dp.s02 = s02;

//运行过程中创建代理对象
Class readObjclass = realObj.getClass();
Object proxy = Proxy.newProxyInstance(readObjclass.getClassLoader(), readObjclass.getInterfaces(), dp);
return proxy;
}

//通过动态代理调用真正的方法的时候就会经过这个方法
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
s01.dress();
cook.cook();
driver.drive();
//干正事!!!!调用委托类里面的具体某一个方法
Object obj = method.invoke(realObj, args);
s02.undress();
cook.cook();

return obj;
}
//不允许外面实例化此类
private DynaProxy(){}

}

我们将MyProxy中的方法抽取出来转移到动态代理中来 就可以删除MyProxy了

跟着在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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<context:annotation-config/>
<context:component-scan base-package="cn.com.leadfar" />
<bean id="me" class="cn.com.leadfar.spring.DynaProxy" factory-method="newInstance">
<constructor-arg ref="realMe"></constructor-arg>
<constructor-arg ref="cook"></constructor-arg>
<constructor-arg ref="driver"></constructor-arg>
<constructor-arg ref="servant01"></constructor-arg>
<constructor-arg ref="servant02"></constructor-arg>
</bean>
<bean id="you" class="cn.com.leadfar.spring.DynaProxy" factory-method="newInstance">
<constructor-arg ref="realYou"></constructor-arg>
<constructor-arg ref="cook"></constructor-arg>
<constructor-arg ref="driver"></constructor-arg>
<constructor-arg ref="servant01"></constructor-arg>
<constructor-arg ref="servant02"></constructor-arg>
</bean>
</beans>

测试代码

public void testplay(){

BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
MeInterface me = (MeInterface) factory.getBean("me");
me.play();
}
public void testwork(){

BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
MeInterface me = (MeInterface) factory.getBean("me");
me.work();
}

 

youFriend

public void test01(){
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
YouInterface you = (YouInterface) factory.getBean("you");
you.playchangcheng();
}

穿衣服
做饭
开车
玩玩去
脱衣服
做饭


public void test02(){
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
YouInterface you = (YouInterface) factory.getBean("you");
you.playgugong();
}

穿衣服
做饭
开车
去故宫游玩
脱衣服
做饭

转载于:https://www.cnblogs.com/oldfish711/p/8351731.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值