Java Proxy动态代理
先看个例子:
接口
package example;
public interface Basic {
public void hello();
}
接口的实现类
package example;
public class BasicService implements Basic {
public void hello() {
Sysytem.out.println("Hello, world");
}
}
实现了InvocationHandler接口的类
package example;
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 Logger logger = Logger.getLogger(this.getClass().getName());
private Object delegate;
public Object bind(Object delegate) {
this.delegate = delegate;
return Proxy.newProxyInstance(delegate.getClass().getClassLoader(), delegate.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
try {
log("method starts..." + method);
result = method.invoke(delegate, args);
log("method ends..." + method);
} catch (Exception e) {
log(e.toString());
}
&nb
相关文档:
package com.test;
public class Jm1 {
private static int asnum;
private static char stchar;
public static void main(String[] args) {
System.out.println(getAsc("A"));
System.out.println(backchar(99));
}
/**
* 字符转ASC
*
* @param st
* @return
*/
public static int get ......
package com.chinacache.boss.queryservice.service.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.chinacache.boss.queryservice.excepti ......
package test;
import java.lang.reflect.Method;
public class InvokeTest {
/**
*
* main 方法
* @param args
* void
*/
public static void main(String[] args) {
try {
&nbs ......
java exception 解决方案 - 我的异常网|异常|exception 730 - org.hibernate.LazyInitializationException:could not initialize proxy 731 - 警告: Error setting value 732 - override and commit 733 - Building workspace has encountered a problem. Errors during build 734 - Could not open the editor: An ......