Spring动态调用


1.package com;  
2.  
3.public interface IComponent {  
4.public void  business1() ;  
5.public void  business2() ;  
6.public void  business3() ;  
7.} 


package com; 


1.public class Component implements IComponent{  
2.    public void business1() {  
3.        System.out.println("业务1");  
4.    }  
5.  
6.    public void business2() {  
7.        System.out.println("业务2");  
8.    }  
9.  
10.    public void business3() {  
11.        System.out.println("业务3");  
12.    }  
13.}  

package com


1.import java.lang.reflect.InvocationHandler;  
2.import java.lang.reflect.Method;  
3.import java.lang.reflect.Proxy;  
4.  
5.public class DynamicProxy implements InvocationHandler {  
6.    private Object obj;  
7.    //private DynamicProxy p;  
8.  
9.    public Object bindl(Object obj) {  
10.        this.obj = obj;  
11.        System.out.println("我是bind*****");  
12.    return  Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj  
13.                .getClass().getInterfaces(),this);// 用来动态装载invoke方法的  
14.        // return new DynamicProxy() ;  
15.        //创建代理类  实例化了一个代理类  
16.    }  
17.  
18.    // 动态调用的 不需要手的设置装载  
19.    public Object invoke(Object proxy, Method method, Object[] args)  
20.            throws Throwable {  
21.        // TODO Auto-generated method stub  
22.        // System.out.println(proxy.getClass());  
23.        // System.out.println(method.getClass());  
24.        Object result = null;  
25.        try {  
26.            valiateUser();  
27.            // System.out.println(proxy.getClass());  
28.            // System.out.println(args);  
29.            // System.out.println(method.getClass());  
30.            result = method.invoke(obj,args);// 调用方法 obj  
31.            // System.out.println(result);  
32.            System.out.println("************");  
33.        } catch (Exception e) {  
34.            e.printStackTrace();  
35.        }  
36.        return result;  
37.    }  
38.  
39.    public void valiateUser() {  
40.        System.out.println("验证用户。。。。");  
41.    }  
42.}  

package com;


1.public class TestD  {  
2.public static void main(String args[]){  
3.    DynamicProxy proxy = new DynamicProxy();  
4.    IComponent component=(IComponent) proxy.bindl(new Component());  
5.    component.business1();//先调用它的invoke方法  再在他的invoke方法里面加载类     
6.    component.business2();  
7.    component.business3();  
8.      
9.      
10.}  
11.}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值