博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
动态代理
阅读量:4575 次
发布时间:2019-06-08

本文共 4945 字,大约阅读时间需要 16 分钟。

//定义接口

public interface Foo
{
void doAction();
}
//实现一
public class FooImpl implements Foo
{
public FooImpl()
{
}

public void doAction()

{
System.out.println("in FooImp1.doAction()");
}
}
//实现二
public class FooImpl2 implements Foo
{
public FooImpl2()
{
}
public void doAction()
{
System.out.println("in FooImp2.doAction()");
}

}

//代理类
public class DynamicSubject implements InvocationHandler
{
private Object sub;

public DynamicSubject()

{
}

public DynamicSubject(Object obj)

{
sub = obj;
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

{
System.out.println("before calling " + method);

method.invoke(sub, args);

System.out.println("after calling " + method);

return null;

}

}

//测试
public class Demo
{
public static void main(String[] args)
{

// 1.通用的动态代理实现

CommonInvocationHandler handler = new CommonInvocationHandler();

Foo f;

// 2.接口实现1

handler.setTarget(new FooImpl());

f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),

new Class[] { Foo.class },

handler);

f.doAction();

// 3.接口实现2

handler.setTarget(new FooImpl2());

f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),

new Class[] { Foo.class },

handler);

f.doAction();

}
}
----------->>也就是面向接口编程,完全可以写一个工厂类实现的,为什么要采用这个方式,如:

class Factory{

Foo fo ;
public Factory(Foo fo){
this.fo = fo;
}
public Foo getInstence(){
return fo;
}
}
然后创建Factory对象
=============》》 有什么差别么??

 

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class AopTest {
public static void main(String[] args) {
Before before = new Before() {
public void before() {
System.out.println("...before...");
}
};
After after = new After() {
public void after() {
System.out.println("...after...");
}
};
Hello hello = null;
// 普通方法执行
System.out.println("-------------普通执行-------------");
hello = new HelloEnglish();
hello.sayHello("bao110908");
hello.sayHi("bao110908");
System.out.println();
// 切入方法执行前(前置增强)
System.out.println("-------------前置增强-------------");
hello = HelloAopManager.getHelloProxy(new HelloEnglish(), before);
hello.sayHello("bao110908");
hello.sayHi("bao110908"); // sayHi 方法没有标注 @Enhancement 所以不会进行代码切入
System.out.println();
// 切入方法执行后(后置增强)
System.out.println("-------------后置增强-------------");
hello = HelloAopManager.getHelloProxy(new HelloEnglish(), after);
hello.sayHello("bao110908");
hello.sayHi("bao110908");
System.out.println();
// 切入方法执行前和执行后(环绕增强)
System.out.println("-------------环绕增强-------------");
hello = HelloAopManager.getHelloProxy(new HelloEnglish(), before, after);
hello.sayHello("bao110908");
hello.sayHi("bao110908");
System.out.println();
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface Enhancement {
}
interface Hello {
@Enhancement
public void sayHello(String name);
public void sayHi(String name);
}
class HelloChinese implements Hello {
public void sayHello(String name) {
System.out.println(name + ",您好");
}
public void sayHi(String name) {
System.out.println("哈啰," + name);
}
}
class HelloEnglish implements Hello {
public void sayHello(String name) {
System.out.println("Hello, " + name);
}
public void sayHi(String name) {
System.out.println("hi, " + name);
}
}
class HelloAopManager {
private HelloAopManager(){
}
public static Hello getHelloProxy(Hello hello, Before before) {
return getHelloProxy(hello, before, null);
}
public static Hello getHelloProxy(Hello hello, After after) {
return getHelloProxy(hello, null, after);
}
public static Hello getHelloProxy(Hello hello, Before before, After after) {
HelloHandler handler = new HelloHandler();
if(before != null) {
handler.setBefore(before);
}
if(after != null) {
handler.setAfter(after);
}
return handler.bind(hello);
}
}
/**
* 前置增强接口
*/
interface Before {
public void before();
}
/**
* 后置增强接口
*/
interface After {
public void after();
}
class HelloHandler implements InvocationHandler {
/**
* 需要进行代理的实例
*/
private Hello hello = null;
/**
* 前置增强
*/
private Before before = null;
/**
* 后置增强
*/
private After after = null;
/**
* InvocationHandler 接口的实现方法,进行动态代理
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// 看看接口中方法是否标注了需要 Enhancement
boolean b = method.isAnnotationPresent(Enhancement.class);
if(!b){
// 没有标注的话,按原方法执行
return method.invoke(hello, args);
}
// 有标注的话,进行方法的前置和后置增强
if(before != null) {
before.before();
}
Object obj = method.invoke(hello, args);
if(after != null) {
after.after();
}
return obj;
}
/**
* 将传入的 Hello 与 InvocationHandler 进行绑定,以获得代理类的实例
* @param hello
* @return
*/
public Hello bind(Hello hello) {
this.hello = hello;
Hello helloProxy = (Hello)Proxy.newProxyInstance(
hello.getClass().getClassLoader(),
hello.getClass().getInterfaces(),
this
);
return helloProxy;
}
public void setAfter(After after) {
this.after = after;
}
public void setBefore(Before before) {
this.before = before;
}
}

 

转载于:https://www.cnblogs.com/peijie-tech/p/3409111.html

你可能感兴趣的文章
wps右键失效_鼠标左键失灵右键代替左键怎么设置
查看>>
cc2530单片机的内核是什么_ZigBee技术开发:CC2530单片机原理及应用简介,目录书摘...
查看>>
学完python基础后该学什么专业_没有基础该怎么学Python,学完后好不好找工作
查看>>
keepalived 多个应用_keepalived 实现 Java 服务的高可用(主备切换)
查看>>
mysql提权 入侵提权软件_【技术】3306端口手动入侵之mysql写入启动项提权
查看>>
windows7 mysql 主从配置_windows下两个mysql5.7的主从配置实现数据库同步
查看>>
mysql多实例 集群_mysqld_multi 快速部署多实例
查看>>
centos 安装mysql脚本_centos7(脚本)安装配置mysql
查看>>
mysql唯一约束和非空_MySQL||唯一约束(Unique Key)和非空约束(NOT NULL)
查看>>
mysql链接失败 驱动_Android连接MySQL问题,驱动加载成功,连接却失败
查看>>
mysql怎么默认utf-8_mysql如何设置默认编码为utf-8
查看>>
业务数据传输用的什么技术保证传输的完整性?_等保2.0 | 安全计算环境之数据完整性、保密性测评...
查看>>
go定时读取mysql_mysql 备份脚本以及定时任务
查看>>
python函数图像平移_python对列进行平移变换的方法(shift)
查看>>
mysql过滤范围_MySQL 过滤数据(WHERE子句)
查看>>
mysql的技术要点_Mysql技术要点:
查看>>
mysql慢查询例子_mysql开启慢查询实例演练(图文)
查看>>
creo显示agent未初始化_三, 初步配置使用zabbix
查看>>
mysql has gone away 自动连接_python下保持mysql连接,避免“MySQL server has gone away“方法...
查看>>
mysql profiling_mysql性能分析-------profiling和explain
查看>>