56 _ implement a configurable AOP framework similar to spring, springaop
"Config. properties configuration file key = Class Name
BeanFactory Bean Factory, responsible for obtaining bean getBean ("xxx ")
"ProxyBeanFactory generate the proxy factory getProxy (Object target, Advice advice );
AopFrameworkTest Test
Ideas:
GetBean ("xxx ")
When xxx = ProxyBeanFactory, the proxy class object is returned.
When xxx = other classes, this class object is directly returned.
#xxx=java.util.ArrayListxxx=com.itcast.day3.aopframework.ProxyBeanFactoryxxx.advice=com.itcast.day3.MyAdvicexxx.target=java.util.ArrayList
Package com. itcast. day3.aopframework; import java. io. IOException; import java. io. inputStream; import java. util. properties; import com. itcast. day3.Advice; public class BeanFactory {private Properties props = new Properties (); public BeanFactory (InputStream ips) {try {props. load (ips);} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}//Bean FactoryPublic Object getBean (String name) {Object bean = null; try {bean = Class. forName (props. getProperty (name )). newInstance (); if (bean instanceof ProxyBeanFactory) {Object proxy = null; ProxyBeanFactory proxyFactoryBean = (ProxyBeanFactory) bean; 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 (); return proxy;} catch (Exception e) {e. printStackTrace ();} return bean ;}}
Package com. itcast. day3.aopframework; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; import com. itcast. day3.Advice; public class ProxyBeanFactory {private Advice advice; // encapsulation of system functions. Generally, there is an interface private Object target; // The target class public Advice getAdvice () {return advice ;} public void setAdvice (Advice advice) {this. advice = advice;} public Object getTarget () {return target;} public void setTarget (Object target) {this.tar get = target;} public Object getProxy () {Object proxy = Proxy. newProxyInstance (target. getClass (). getClassLoader (), // target class loader of the target class. getClass (). getInterfaces (), // interface implemented by the target class new InvocationHandler () {// anonymous internal class @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {advice. beforeMethod (method); Object reVal = method. invoke (target, args); // call the method advice of the target class in reflection mode. afterMethod (method); return reVal ;}}); return proxy ;}}
Package com. itcast. day3.aopframework; import java. io. inputStream; import java. lang. reflect. type; import java. util. arrayList; import java. util. collection; public class AopFrameworkTest {public static void main (String [] args) throws Exception {InputStream ips = AopFrameworkTest. class. getResourceAsStream ("config. properties "); Object bean = new BeanFactory (ips ). getBean ("xxx"); if (bean instanceof ArrayList) {Collection collection = (Collection) bean); collection. add ("123"); for (Object obj: collection) {System. out. println (obj) ;}} else {System. out. println ("the returned proxy class is:" + bean. getClass (). getName () + "instance"); (Collection) bean ). clear ();}}}