文章标题
来源:互联网 发布:网站中文域名注册 编辑:程序博客网 时间:2024/06/14 16:48
动态代理
package com.ldu.reflect;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;interface Subject { String action();}class RealSubject implements Subject { @Override public String action() { System.out.println("我是代理类!!!!!!!!!!!!"); return "代理类"; }}class AopTest{ public void a1(){ System.out.println("i am a1"); } public void a2(){ System.out.println("i am a2"); }}class myInvocationHandler implements InvocationHandler { Object obj; public Object blind(Object obj) { this.obj = obj; // 返回一个代理类的对象(被代理类的类加载器,被代理类实现的接口,调用此方法的对象) return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj .getClass().getInterfaces(), this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { AopTest at=new AopTest(); at.a1(); Object resultValue = method.invoke(obj, args); at.a2(); return resultValue; }}public class TestDongTai { public static void main(String[] args) { RealSubject rs=new RealSubject(); //创建一个实现了InvocationHandler接口的类的对象 myInvocationHandler mih=new myInvocationHandler(); Object obj=mih.blind(rs); Subject sub=(Subject)obj; String str=sub.action(); System.out.println(str); System.out.println("------------------------------"); String str1=rs.action(); System.out.println(str1); }}
阅读全文
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 如何完全的卸载MySQL数据库?
- Flume与Kafka整合案例详解
- windows环境下安装zookeeper
- Java--线程的分类和生命周期
- for循环、php可变变量
- 文章标题
- Python的安装设置及print函数
- projectforge研究第三天:采用Wicket框架,HSQLDB数据库
- 连续第七日总结
- Piwigo画廊的安装
- mysql学习资料
- Trie树(单词查找树)
- Robert E. Tarjan——杰出计算机科学家
- SpringMVC框架使用(一)