56 _ implement a configurable AOP framework similar to spring, springaop

Source: Internet
Author: User

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.

 

 

  • Config. properties
        
#xxx=java.util.ArrayListxxx=com.itcast.day3.aopframework.ProxyBeanFactoryxxx.advice=com.itcast.day3.MyAdvicexxx.target=java.util.ArrayList

 

  • BeanFactory
      
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 ;}}
  • ProxyBeanFactory
  

 

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

 

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

Related Article

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.