public class ProxyHandler implements InvocationHandler {
private Object target;
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result = null;
if(method.getName().equals("info")){
System.out.println("事务开始");
result = method.invoke(target, args);
System.out.println("事务结束");
}
return result;
}
public void setTarget(Object o){
this.target = o;
}
}
解耦合
public class MyProxyFactory {
public static Object getProxy(Object object){
ProxyHandler handler = new ProxyHandler();
handler.setTarget(object);
return Proxy.newProxyInstance(DogImpl.class.getClassLoader(),DogImpl.class.getInterfaces(), handler);
}
}