Java Dynamic proxy imitates Spring AOP

Source: Internet
Author: User
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 ());}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.