InvocationHandler 操作例子(切面概念)
来源:互联网 发布:小米随身wifi mac驱动 编辑:程序博客网 时间:2024/05/17 02:57
public interface IHello {
public void hello(String name);
}
--------------------------------------------------------------------------------
public class HelloSpeaker implements IHello {
public void hello(String name) {
System.out.println("Hello, " + name);
}
}
--------------------------------------------------------------------------------
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LogHandler implements InvocationHandler {
private Object delegate;
private Logger logger = Logger.getLogger(this.getClass().getName());
public Object bind(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 result = null;
try {
log("method starts..." + method);
result = method.invoke(delegate, args);
logger.log(Level.INFO, "method ends..." + method);
} catch (Exception e) {
log(e.toString());
}
return result;
}
private void log(String message) {
logger.log(Level.INFO, message);
}
}
--------------------------------------------------------------------------------
public class ProxyDemo {
public static void main(String[] args) {
LogHandler logHandler = new LogHandler();
IHello helloProxy = (IHello) logHandler.bind(new HelloSpeaker());
helloProxy.hello("Justin");
}
』
好了,拷贝上面的自己运行看看就知道了,原来切面就是这么回事。
public void hello(String name);
}
--------------------------------------------------------------------------------
public class HelloSpeaker implements IHello {
public void hello(String name) {
System.out.println("Hello, " + name);
}
}
--------------------------------------------------------------------------------
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LogHandler implements InvocationHandler {
private Object delegate;
private Logger logger = Logger.getLogger(this.getClass().getName());
public Object bind(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 result = null;
try {
log("method starts..." + method);
result = method.invoke(delegate, args);
logger.log(Level.INFO, "method ends..." + method);
} catch (Exception e) {
log(e.toString());
}
return result;
}
private void log(String message) {
logger.log(Level.INFO, message);
}
}
--------------------------------------------------------------------------------
public class ProxyDemo {
public static void main(String[] args) {
LogHandler logHandler = new LogHandler();
IHello helloProxy = (IHello) logHandler.bind(new HelloSpeaker());
helloProxy.hello("Justin");
}
』
好了,拷贝上面的自己运行看看就知道了,原来切面就是这么回事。
- InvocationHandler 操作例子(切面概念)
- InvocationHandler的一个例子
- InvocationHandler的一个例子
- SpringAOP的概念(面向切面)
- 面向切面编程(实现例子)
- InvocationHandler
- InvocationHandler
- InvocationHandler
- InvocationHandler
- java动态代理例子(Proxy,InvocationHandler)
- Spring----AOP面向切面编程例子解析(附源码)
- Spring----AOP面向切面编程例子解析(附源码)
- Spring的切面编程例子
- java代理(AOP) InvocationHandler
- arcgis engine 图形操作函数 (面切面)
- Spring的控制反转(IoC)和面向切面编程(AOP)的概念浅析。
- AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)
- AOP那些学术概念—通知、增强处理连接点(JoinPoint)切面(Aspect)
- VB.NET MDI画面中变化子画面显示的方法
- SQL Injection(三)――Blind Injection
- JAVA代理读取oracle异常
- GWT生成JavaScript代码文件分析
- Win32.LostLove 病毒分析及清除
- InvocationHandler 操作例子(切面概念)
- 常用tcp/udp端口
- 比尔盖茨的10大优秀员工准则——职业人必读!
- 这个是csdn的"不完全log"查看站吧可惜只有四个月的()
- 笔记:内核中的互斥
- 黑客反汇编高速入门
- Argument Dependent Lookup (ADL, a.k.a. Koenig Lookup) 解析 (3)
- 忽然间的感悟...
- CSDN日照之旅