今天利用一个简单的例子理解一下JAVA反射中的Proxy类即代理类,以及体会代理机制的优点
通常我们运行代码时会采用如下写法:
package com.icss.biz;
import java.util.Date;
public class TestProxy {
@Performance
public static void testTime1() {
long startTime=new Date().getTime();
System.out.println("执行代码1");
System.out.println("执行代码2");
System.out.println("执行代码3");
System.out.println("执行代码4");
System.out.println("执行代码5");
long endTime=new Date().getTime();
long totalTime=endTime-startTime;
System.out.println("执行时间----------"+totalTime);
}
public static void testTime2() {
long startTime=new Date().getTime();
System.out.println("执行代码1");
System.out.println("执行代码2");
System.out.println("执行代码3");
System.out.println("执行代码4");
System.out.println("执行代码5");
long endTime=new Date().getTime();
long totalTime=endTime-startTime;
System.out.println("执行时间----------"+totalTime);
}
public static void testTime3() {
long startTime=new Date().getTime();
System.out.println("执行代码1");
System.out.println("执行代码2");
System.out.println("执行代码3");
System.out.println("执行代码4");
System.out.println("执行代码5");
long endTime=new Date().getTime();
long totalTime=endTime-startTime;
System.out.println("执行时间---------"+totalTime);
}
public static void main(String[] args) {
testTime1();
testTime2();
testTime3();
}
}
代码执行时会逐行执行,当我们完成较大项目如上千行代码时,如上的编程方法就会显得冗杂,此时就会有一个很便捷的机制,即代理机制。如下
1、设置代理机制DyProxy();
package com.icss.biz;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; //动态代理机制
import java.util.Date;
/**
*
* @author doris-che
*
*/
public class DyProxy {
private Object toProxyob;
public Object getProxyObject(Object object) {
this.toProxyob=object;
return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result;
if (method.isAnnotationPresent(Performance.class)) { //判断是否有注解
long startTime=new Date().getTime();
result=method.invoke(toProxyob, args);
long endTime=new Date().getTime();
long totalTime=endTime-startTime;
System.out.println("调用时间"+totalTime);
}else {
result=method.invoke(toProxyob, args);
}
return result;
}
});
}
}
2、使用代理机制调用UserService()中的方法
package com.icss.biz;
public class TestAop {
public static void main(String[] args) {
UserService us=(UserService)new DyProxy().getProxyObject(new UserServiceImpl()) ;
us.login();
us.logout();
us.getUserList();
}
}
总结
使用Proxy类完成代码执行,只需设置一个代理机制,在main()方法中调用代理机制,即可执行全部代码。非常便捷。
附件:
1、UserService.java
package com.icss.biz;
public interface UserService {
void login();
@Performance
void logout();
void getUserList();
}
2、UserServiceimpl.java
package com.icss.biz;
public class UserServiceImpl implements UserService{
@Override
public void login() {
System.out.println("调用login");
System.out.println("调用login");
System.out.println("调用login");
System.out.println("调用login");
System.out.println("调用login");
}
@Override
public void logout() {
System.out.println("调用logout");
System.out.println("调用logout");
System.out.println("调用logout");
System.out.println("调用logout");
System.out.println("调用logout");
}
@Override
public void getUserList() {
System.out.println("调用getUserList");
System.out.println("调用getUserList");
System.out.println("调用getUserList");
System.out.println("调用getUserList");
System.out.println("调用getUserList");
}
}
3、注解
package com.icss.biz;
import static java.lang.annotation.ElementType.METHOD;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(value=RetentionPolicy.RUNTIME)
@Target(METHOD)
public @interface Performance {
}