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 ();
}
}