Advice Plane Interface
package aop;import java.lang.reflect.Method;public interface Advice {void beforeMethed(Method method);void afterMethed(Method method);}Implementation class of myadvice plane
package aop;import java.lang.reflect.Method;public class MyAdvice implements Advice {private long beginTime=0;@Overridepublic void beforeMethed(Method method) {beginTime=System.currentTimeMillis();System.out.println("before method:"+method.getName());}@Overridepublic void afterMethed(Method method) {long endTime=System.currentTimeMillis();System.out.println("end method:"+method.getName()+" method use "+String.valueOf(endTime-beginTime)+" millisecond");}}Agent Factory
Package AOP; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method; import Java. lang. reflect. proxy; public class proxfactorybean {private object target; private advice; public object getprox () {object prox = proxy. newproxyinstance (target. getclass (). getclassloader (), // Class Loader target. getclass (). getinterfaces (), // interface of the target object new invocationhandler () {@ overridepublic object invoke (Object proxy, method, object [] ARGs) throws throwable {advice. beforemethed (method); // object resval = method before calling the target. invoke (target, argS); // call the target advice. aftermethed (method); // return resval after the target is called;}); Return prox; // return the interface of the target object, therefore, you must use the interface} public void settarget (object target) implements this.tar get = target;} public void setadvice (advice) {This. advice = advice ;}}
Bean Factory
Package AOP; import Java. io. ioexception; import Java. io. inputstream; import Java. util. properties; public class beanfactory {private properties Properties = new properties (); Public beanfactory (inputstream instream) throws ioexception {properties. load (instream);} public object getbean (string beanname) {string classname = properties. getproperty (beanname); // find the full bean path in the configuration file object bean = NULL; try {bean = Class. forname (classname ). newinstance (); // instantiate bean} catch (exception e) {e. printstacktrace ();} If (bean instanceof arguments) {// If the bean is a proxy object proxfactorybean = (proxfactorybean) bean; // obtain the proxy object try {advice = (advice) class. forname (properties. getproperty (beanname + ". advice ")). newinstance (); object target = Class. forname (properties. getproperty (beanname + ". target ")). newinstance (); proxfactorybean. setadvice (advice); // you can specify proxfactorybean. settarget (target); // set the proxy object} catch (exception e) {e. printstacktrace ();} return proxfactorybean. getprox () ;}return bean ;}}
Configuration File
#xxx=java.util.ArrayListxxx=aop.ProxFactoryBeanxxx.advice=aop.MyAdvicexxx.target=java.util.ArrayList
Test class
Package AOP; import Java. io. ioexception; import Java. util. arraylist; import Java. util. collection; public class aopmain {/*** @ Param ARGs * @ throws ioexception */public static void main (string [] ARGs) throws ioexception {beanfactory factory = new beanfactory (aopmain. class. getresourceasstream ("bean. properties "); collection list = (Collection) factory. getbean ("XXX"); // The interface list is used here. add ("A"); system. out. println (list. size ());}}