????????????????????InvocationHandler??invoke()????????????????????????????????????????——???????????????е??????в???????????????Щ????????????????aop??before??after??around????hook??????????????????add multiple aops?????????Handler???Aspect???????????????????????????μ???軌?????????????????????????????????
??????????????????????aop??????????????
???????????BeforeHandler ?? AfterHandler??????aop????????????????????????????????????н?????????
?????????????????????е?aop handler??????????????????????????????hook??
???????????????????????????й????
???????????????????????????????proxy object????
?????????????
????????????????????????Σ?????????и????????????·?????????????????????
???????????handler???????
????public abstract class AbstractHandler implements InvocationHandler {
????private Object targetObject;
????public void setTargetObject(Object targetObject) {
????this.targetObject = targetObject;
????}
????}
???????????BeforeHandler??AfterHandler?????????????handler??????
????public abstract class BeforeHandler extends AbstractHandler {
????public abstract void handleBefore(Object proxy?? Method method?? Object[] args);
????public Object invoke(Object proxy?? Method method?? Object[] args) throws Throwable {
????handleBefore(proxy?? method?? args);
????return method.invoke(getTargetObject()?? args);
????public abstract class AfterHandler extends AbstractHandler {
????public abstract void handleAfter(Object proxy?? Method method?? Object[] args);
????public Object invoke(Object proxy?? Method method?? Object[] args) throws Throwable {
????Object result = method.invoke(getTargetObject()?? args);
????handleAfter(proxy?? method?? args);
????return result;
????}
????}
????????Proxy???????
????public class ProxyFactory {
????public static Object getProxy(Object targetObject??
????List handlers) {
????//Code to get the proxy
????return proxyObject;
????} else {
????return targetObject;
????}
????}
????}
????????????????
????CalculatorImpl calcImpl = new CalculatorImpl();
????BeforeHandler before = new BeforeHandlerImpl();
????AfterHandler after = new AfterHandlerImpl();
????List<AbstractHandler> handlers = new ArrayList<AbstractHandler>();
????handlers.add(before);
????handlers.add(after);
????Calculator proxy = (Calculator) ProxyFactory.getProxy(calcImpl??
????handlers);
????int result = proxy.calculate(20?? 10);
????????
??????????????μ?????????????AOP??????????structural implementation????????????????????????????????????????????ò???????????????????????л????????????????????Java???????????????????????е????????????Ч????
????https://github.com/debjava/aopusingjdkdynamicproxy
???????
???????????????й?AOP?????????????????????????????????????before??after????aop???????????????“Around”??“Throw”?????????????????ɡ?