zoukankan
html css js c++ java
最简单的动态代理实例(spring基于接口代理的AOP原理)
JDK的动态代理是基于接口的
package
com.open.aop;
public
interface
BusinessInterface
{
public
void
processBusiness();
}
目标对象
package
com.open.aop;
public
class
BusinessObject
implements
BusinessInterface
{
public
void
processBusiness()
{
System.out.println(
"
action
"
);
}
}
代理对象
package
com.open.aop;
import
java.lang.reflect.InvocationHandler;
import
java.lang.reflect.Method;
import
java.lang.reflect.Proxy;
public
class
LogHandler
implements
InvocationHandler
{
private
Object delegate;
public
Object binder(Object delegate)
{
this
.delegate
=
delegate;
return
Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
delegate.getClass().getInterfaces(),
this
);
}
public
Object invoke(Object proxy, Method method, Object[] args)
throws
Throwable
{
Object obj
=
null
;
System.out.println(
"
对象方法调用前
"
);
try
{
obj
=
method.invoke(delegate, args);
}
catch
(Exception e)
{
}
System.out.println(
"
对象方法调用后
"
);
return
obj;
}
}
测试类
package
com.open.aop;
import
java.lang.reflect.InvocationHandler;
import
java.lang.reflect.Proxy;
public
class
Test
{
public
static
void
main(String[] args)
{
BusinessInterface bi
=
(BusinessInterface)
new
LogHandler()
.binder(
new
BusinessObject());
bi.processBusiness();
}
}
输出结果
对象方法调用前
action
对象方法调用后
查看全文
相关阅读:
fetch API 和 ajax
java 通过数据库名获得 该数据所有的表名以及字段名、字段类型
自定义注解,通过反射获得注解中的值(详细自定义注解解释)
main方法中sleep
eclipse中设置JVM内存
命令java 找不到或无法加载主类
windows下的命令
mac terminal基本命令
ThreadLocal 源码剖析
SQL中的函数用法
原文地址:https://www.cnblogs.com/shaohz2014/p/3582151.html
最新文章
JVM集训-----内存结构
ZooKeePer总汇
Spring-Mybatis-SpringMVC三大框架整合
SpringMVC---------数据校验
SpringMVC-----拦截器
Spring MVC-------文件上传,单文件,多文件,文件下载
python系列五:Python3列表list
python系列三:python3运算符
ubuntu 16.04 启用root用户方法
Ubuntu 16.04特性及使用基本方法
热门文章
Ubuntu 16.04安装各种软件
python系列二:python3基本数据类型
junit5荟萃知识点(一):junit5的组成及安装
python错误笔记
SpringBoot + JPA + mariadb
Scarpy框架安装教程
pip 升级
Python 模块之间的引用
记忆
冒泡排序(逻辑思维题练习)
Copyright © 2011-2022 走看看