代理Handler 通用类

xiaoxiao2024-07-24  39

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); } }

 

转载请注明原文地址: https://www.6miu.com/read-5017090.html

最新回复(0)