??????????????
?????????????????Ч????????????????????????????????????????????????????????????????????????????????????????????????????????????InvocationHandler?У??????????????????е??????
????InvocationHandler?????
????import java.lang.reflect.InvocationHandler;
????import java.lang.reflect.Method;
????public class PerformanceInterceptor implements InvocationHandler {
????private Object proxied;
????public PerformanceInterceptor(Object proxied) {
????this.proxied = proxied;
????}
????@Override
????public Object invoke(Object proxy?? Method method?? Object[] args) throws Throwable {
????long startTime = System.currentTimeMillis();
????Object obj = method.invoke(proxied?? args);
????long endTime = System.currentTimeMillis();
????System.out.println("Method " + method.getName() + " execution time: " + (endTime - startTime) * 1.0 / 1000 + "s");
????return obj;
????}
????}
??????????
????import java.lang.reflect.InvocationHandler;
????import java.lang.reflect.Proxy;
????import org.junit.Test;
????public class PerformanceInterceptorTest {
????@Test
????public void testInvoke() {
????UserDao userDao = new UserDaoImpl();
????Class<?> cls = userDao.getClass();
????InvocationHandler handler = new PerformanceInterceptor(userDao);
????UserDao proxy = (UserDao) Proxy.newProxyInstance(cls.getClassLoader()?? cls.getInterfaces()?? handler);
????proxy.addUser(new User("tom"));
????proxy.deleteUser("tom");
????proxy.updateUser("tom");
????}
????}
??????????
????add user named Tom...
????Method addUser execution time: 1.0s
????delete user named tom...
????Method deleteUser execution time: 1.5s
????update user named tom...
????Method updateUser execution time: 2.0s
???????????
??????????????????????????к???н????????Щ?????????????????????з????????Щ???????м???????????д??????????
????InvocationHandler?????
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class LogInterceptor implements InvocationHandler {
private Object proxied;
public static final String path = "run.log";
public LogInterceptor(Object proxied) {
this.proxied = proxied;
}
public String beforeMethod(Method method) {
return getFormatedTime() + " Method:" + method.getName() + " start running ";
}
public String afterMethod(Method method) {
return getFormatedTime() + " Method:" + method.getName() + " end running ";
}
@Override
public Object invoke(Object proxy?? Method method?? Object[] args) throws Throwable {
write(path?? beforeMethod(method));
Object object = method.invoke(proxied?? args);
write(path?? afterMethod(method));
return object;
}
public String getFormatedTime() {
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return formater.format(System.currentTimeMillis());
}
public void write(String path?? String content) {
FileWriter writer = null;
try {
writer = new FileWriter(new File(path)?? true);
writer.write(content);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != writer) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
??????????
????import java.lang.reflect.InvocationHandler;
????import java.lang.reflect.Proxy;
????import org.junit.Test;
????public class LogInterceptorTest {
????@Test
????public void testInvoke() {
????UserDao userDao = new UserDaoImpl();
????Class<?> cls = userDao.getClass();
????InvocationHandler handler = new LogInterceptor(userDao);
????UserDao proxy = (UserDao) Proxy.newProxyInstance(cls.getClassLoader()?? cls.getInterfaces()?? handler);
????proxy.addUser(new User("tom"));
????proxy.deleteUser("tom");
????proxy.updateUser("tom");
????}
????}
??????????
??????????????????run.log????????????£?
????2015-10-07 05:41:02 Method:addUser start running
????2015-10-07 05:41:03 Method:addUser end running
????2015-10-07 05:41:03 Method:deleteUser start running
????2015-10-07 05:41:05 Method:deleteUser end running
????2015-10-07 05:41:05 Method:updateUser start running
????2015-10-07 05:41:07 Method:updateUser end running
???????
????????Java????????????????????????????в???????????Java???????????????????????????????????κν???????д?????????????????????????cdlib??Java???????????????AOP????????????????????????????????????????????????Щ??????????????????????????????Щ?????????