Original Spring AOP Compact version

Source: Internet
Author: User
Tags aop compact stub

JavaBean must have a constructor with no parameters


Config.properties

#xxx =java.util.arraylist
Xxx=cn.itcast.day3.aopframework.proxyfactorybean
Xxx.advice=cn.itcast.day3.myadvice
Xxx.target=java.util.arraylist


Beanfactory.java

public class Beanfactory {
Properties Props = new properties ();
Public beanfactory (InputStream IPs) {
try {
Props.load (IPS);
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

Public Object Getbean (String name) {
String className = props.getproperty (name);
Object bean = null;
try {
Class clazz = Class.forName (className);
Bean = Clazz.newinstance ();
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
if (bean instanceof Proxyfactorybean) {
Object proxy = null;
Proxyfactorybean Proxyfactorybean = (Proxyfactorybean) bean;
try {
Advice Advice = (Advice) class.forname (props.getproperty (name + ". Advice")). newinstance ();
Object target = Class.forName (Props.getproperty (name + ". Target")). newinstance ();
Proxyfactorybean.setadvice (advice);
Proxyfactorybean.settarget (target);
Proxy = Proxyfactorybean.getproxy ();
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return proxy;
}
return bean;
}
}


Advice.java

Public interface Advice {
void Beforemethod (method method);
void Aftermethod (method method);
}

Myadvice.java

public class Myadvice implements Advice {
Long begintime = 0;
public void Aftermethod (method method) {
TODO auto-generated Method Stub
System.out.println ("Graduated to work.") ");
Long endtime = System.currenttimemillis ();
System.out.println (Method.getname () + "Running Time of" + (Endtime-begintime));

}

public void Beforemethod (method method) {
TODO auto-generated Method Stub
System.out.println ("to learn.") ");
BeginTime = System.currenttimemillis ();
}

}


Proxyfactorybean.java

public class Proxyfactorybean {

Private Advice Advice;
Private Object target;

Public Advice Getadvice () {
return advice;
}

public void Setadvice (Advice Advice) {
This.advice = advice;
}

Public Object Gettarget () {
return target;
}

public void Settarget (Object target) {
This.target = target;
}

Public Object GetProxy () {
TODO auto-generated Method Stub
Object Proxy3 = proxy.newproxyinstance (
Target.getclass (). getClassLoader (),
/*new class[]{collection.class},*/
Target.getclass (). Getinterfaces (),
New Invocationhandler () {

public object invoke (object proxy, Method method, object[] args)
Throws Throwable {

/*long begintime = System.currenttimemillis ();
Object RetVal = Method.invoke (target, args);
Long endtime = System.currenttimemillis ();
System.out.println (Method.getname () + "Running Time of" + (Endtime-begintime));
Return retval;*/


Advice.beforemethod (method);
Object RetVal = Method.invoke (target, args);
Advice.aftermethod (method);
return retVal;

}
}
);
return proxy3;
}

}


Aopframeworktest.java

Public class Aopframeworktest {

    /**
     * @param args
   & nbsp */
    public static void Main (string[] args) throws Exception {
       &n bsp;//TODO auto-generated Method stub
        inputstream ips = AopFrameworkTest.class.getResourceAsStream ("Config.properties");
        object bean = new Beanfactory (IPS). Getbean ("xxx");
        system.out.println (Bean.getclass (). GetName ());
         ((Collection) bean). Clear ();
    }

}

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.