public interface Subject { void doSomething(String str); }
public class RealSubject implements Subject { @Override public void doSomething(String str) { System.out.println("do something..." + str); } }
public interface IAdvice { void exec(); }
public class BeforeAdvice implements IAdvice { @Override public void exec() { System.out.println("前置通知被执行!"); } }
public class AfterAdvice implements IAdvice { @Override public void exec() { System.out.println("后置通知被执行!"); } }
public class MyInvocationHandler implements InvocationHandler { private Subject realSubject;
public MyInvocationHandler(Subject realSubject) { this.realSubject = realSubject; }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { IAdvice beforeAdvice = new BeforeAdvice(); beforeAdvice.exec(); Object result = method.invoke(this.realSubject, args); IAdvice afterAdvice = new AfterAdvice(); afterAdvice.exec(); return result; } }
public class DynamicProxy { public static T newProxyInstance(ClassLoader classLoader, Class>[] interfaces, InvocationHandler handler) { @SuppressWarnings("unchecked") T t = (T) Proxy.newProxyInstance(classLoader, interfaces, handler); return t; } }
public class AOPClient { public static void main(String[] args) { Subject realSubject = new RealSubject(); InvocationHandler handler = new MyInvocationHandler(realSubject); ClassLoader classLoader = realSubject.getClass().getClassLoader(); Class>[] interfaces = realSubject.getClass().getInterfaces(); Subject proxySubect = DynamicProxy.newProxyInstance(classLoader, interfaces, handler); proxySubect.doSomething("这是一个Dynamic AOP示例!!!"); } } ```
Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ...
[详细]