Static agents
Step one: create an abstract themes (interfaces)
Step two: the real business code
Step three: Test
JDK agent
Step one: create an abstract themes (interfaces)
Step two: the real business code
Step three: Test
CGLIB agent
Step a: Code layer
Step two: Test
{class ServiceCglib public public static void main (String [] args) { // Step a: audiences Final Cglib new new Cglib CGLIB = (); // Step two: generating agent Enhancer Enhancer Enhancer new new = (); // Step Three : Specifies the target object proxy timber formwork enhancer.setSuperclass (cglib.getClass ()); // step four: achieving enhanced processing operation enhancer.setCallback (MethodInterceptor new new () { / ** * * @param O audiences * @param method the target method parameter * @param target method of objects * @param method methodProxy proxy target object * @return * @throws Throwable * / @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println("后置增强===========");
Object invoke = methodProxy.invoke(cglib, objects);
System.out.println("后置增强==========="); return invoke; } });
// The final step: creating a proxy Cglib o = (Cglib) enhancer.create (); o.Some ();}}