转载

【java实例】自己动手实现IOC和MVC(四)

现在我要进行实例化的信息都收集起来,然后进行实例化了 问题? ①怎么进行实例化? 这时候我要用到我们收集的class的信息类的List,进行循环 ,然后利用Java反射机制进行实例化操作 下面一段伪代码:
  1. List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
  2.      if(beanDefinitions!=null && !beanDefinitions.isEmpty()){
  3.             for(BeanDefinition bd:beanDefinitions){
  4.                 if(bd!=null){
  5.                     //初始化bean
  6.                     //然后是存储bean
  7.                 }
  8.             }
  9.     }
  ②怎么进行注入? 也是循环List<BeanDefinition> ,然后循环BeanDefinition里面的属性集合,接着利用java反射调用set方法进行注入操作
  1. List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
  2.         for(BeanDefinition bd : beanDefinitions){
  3.             Object bean =获得实例化后的bean根据id
  4.             List<PropertyDefinition> pds = bd.getProperties();
  5.             if(pds!=null && !pds.isEmpty()){
  6.                 for(PropertyDefinition pd:pds){
  7.                     String name = pd.getName();
  8.                     String ref = pd.getRef();
  9.                     if(ref!=null && !"".equals(ref)){
  10.                         //这里会进行属性的注入操作
  11.                     }
  12.                 }
  13.             }
  14.         }
②实例化之后放在哪里? 会放在一个Map里 ,供我们以后进行使用 这里我们提供一个类用于对这个Map的操作 ,具体实现如下:
  1. package com.ajunframework.beans.factory;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. /**
  5.  * 存储bean
  6.  * @author harries
  7.  *
  8.  */
  9. public class BeanMap {
  10.     private static Map<String,Object> beans = new HashMap<String,Object>();
  11.     private BeanMap(){}
  12.     /**
  13.      * 添加bean
  14.      * @param beanName
  15.      * @param value
  16.      */
  17.     public  static void put(String beanName ,Object value){
  18.         beans.put(beanName, value);
  19.     }
  20.     /**
  21.      * 获得bean
  22.      * @param beanName
  23.      * @return
  24.      */
  25.     public static Object getBean(String beanName){
  26.         if(!beans.isEmpty()){
  27.             return beans.get(beanName);
  28.         }
  29.         return new HashMap<String,Object>();
  30.     }
  31.     public static Map<String,Object> getBeanMap(){
  32.         return beans;
  33.     }
  34. }
此时提供了一个BeanFactory interface 用于我们扩展 对beanMap的操作
  1. package com.ajunframework.beans.factory;
  2. /**
  3.  *bean工厂
  4.  * @author harries
  5.  * @http://www.liuhaihua.cn
  6.  */
  7. public interface BeanFactory {
  8.     /**
  9.      * 设置bean
  10.      * @param beanName 
  11.      * @param fullClassName
  12.      */
  13.      void setBean(String beanName,String fullClassName);
  14.     /**
  15.      * 获得bean根据beanName
  16.      * @param name
  17.      * @return
  18.      */
  19.     Object getBean(String name);
  20.     <T> T getBean(String name, Class<T> requiredType);
  21.     <T> T getBean(Class<T> requiredType);
  22.     Object getBean(String name, Object... args);
  23.     boolean containsBean(String name);
  24.     boolean isSingleton(String name);
  25.     boolean isPrototype(String name);
  26.     @SuppressWarnings("unchecked")
  27.     boolean isTypeMatch(String name, Class targetType);
  28.     Class<?> getType(String name);
  29.     String[] getAliases(String name);
  30. }
下面是实现类 AnnotationBeanFactory.java
  1. package com.ajunframework.beans.factory;
  2. import com.ajunframework.beans.annotation.Action;
  3. import com.ajunframework.beans.annotation.Dao;
  4. import com.ajunframework.beans.annotation.Scope;
  5. import com.ajunframework.beans.annotation.Service;
  6. import com.ajunframework.beans.constant.BeanScop;
  7. import com.ajunframework.beans.utils.BeanUtils;
  8. /**
  9.  * bean工厂
  10.  * @author harries
  11.  * @http://www.liuhaihua.cn
  12.  */
  13. public class AnnotationBeanFactory implements BeanFactory {
  14.     private static AnnotationBeanFactory beanFactory = new AnnotationBeanFactory();
  15.     private AnnotationBeanFactory(){}
  16.     public static AnnotationBeanFactory getBeanFactory(){
  17.         if(beanFactory!=null){
  18.             return beanFactory;
  19.         }
  20.         return new AnnotationBeanFactory();
  21.     }
  22.     public void setBean(String beanName,String fullClassName) {
  23.         try {
  24.             Object bean = BeanUtils.instanceClass(Class.forName(fullClassName));
  25.             BeanMap.put(beanName, bean);
  26.         } catch (ClassNotFoundException e) {
  27.             e.printStackTrace();
  28.         }
  29.     }
  30.     public boolean containsBean(String name) {
  31.         Object bean = BeanMap.getBeanMap().get(name);
  32.         if(bean!=null){
  33.             return true;
  34.         }
  35.         return false;
  36.     }
  37.     public Object getBean(String name) {
  38.         return BeanMap.getBeanMap().get(name);
  39.     }
  40.     @SuppressWarnings("unchecked")
  41.     public <T> T getBean(String name, Class<T> requiredType) {
  42.         Object bean = BeanMap.getBeanMap().get(name);
  43.         if(bean!=null){
  44.             Class<?> clazz = bean.getClass();
  45.             if(clazz.getName().equals(requiredType.getName()) ){
  46.                 return (T)bean;
  47.             }
  48.         }
  49.         return null;
  50.     }
  51.     @SuppressWarnings("unchecked")
  52.     public <T> T getBean(Class<T> requiredType) {
  53.         if(requiredType.isAnnotationPresent(Dao.class) || requiredType.isAnnotationPresent(Service.class) || requiredType.isAnnotationPresent(Action.class)){
  54.             String fullName = requiredType.getName();
  55.             String beanName = fullName.substring(fullName.lastIndexOf(".")+1).substring(0,1).toLowerCase()+ fullName.substring(fullName.lastIndexOf(".")+1).substring(1);
  56.             Object bean = BeanMap.getBeanMap().get(beanName);
  57.             return (T)bean;
  58.         }
  59.         return null;
  60.     }
  61.     public Object getBean(String name, Object... args) {
  62.         return null;
  63.     }
  64.     public Class<?> getType(String name) {
  65.         Object bean = BeanMap.getBeanMap().get(name);
  66.         if(bean!=null){
  67.             bean.getClass().getClass();
  68.         }
  69.         return null;
  70.     }
  71.     @SuppressWarnings("unchecked")
  72.     public boolean isPrototype(String name) {
  73.         Object bean = BeanMap.getBeanMap().get(name);
  74.         if(bean!=null){
  75.             Class clazz = bean.getClass();
  76.             if(clazz.isAnnotationPresent(Scope.class)){
  77.                 String scopeValue = ((Scope) clazz.getAnnotation(Scope.class)).value();
  78.                 if(scopeValue!=null && BeanScop.PROTOTYPE.equals(scopeValue.trim())){
  79.                     return true;
  80.                 }
  81.             }
  82.         }
  83.         return false;
  84.     }
  85.     @SuppressWarnings("unchecked")
  86.     public boolean isSingleton(String name) {
  87.         Object bean = BeanMap.getBeanMap().get(name);
  88.         if(bean!=null){
  89.             Class clazz = bean.getClass();
  90.             if(clazz.isAnnotationPresent(Scope.class)){
  91.                 String scopeValue = ((Scope) clazz.getAnnotation(Scope.class)).value();
  92.                 if(scopeValue!=null && BeanScop.SINGLETON.equals(scopeValue.trim())){
  93.                     return true;
  94.                 }else if(scopeValue!=null && BeanScop.PROTOTYPE.equals(scopeValue.trim())){
  95.                     return false;
  96.                 }else{
  97.                     return true;
  98.                 }
  99.             }else{
  100.                 return true;
  101.             }
  102.         }
  103.         return false;
  104.     }
  105.     @SuppressWarnings("unchecked")
  106.     public boolean isTypeMatch(String name, Class targetType) {
  107.         Object bean = BeanMap.getBeanMap().get(name);
  108.         if(bean!=null){
  109.             Class clazz = bean.getClass();
  110.             if(clazz.equals(targetType)){
  111.                 return true;
  112.             }
  113.         }
  114.         return false;
  115.     }
  116.     public String[] getAliases(String name) {
  117.         return null;
  118.     }
  119. }
下面给出上面整个过程的代码: 初始化上下文bean 接口
  1. package com.ajunframework.beans.applicationContext;
  2. /**
  3.  * 初始化上下文bean 接口
  4.  * @author harries
  5.  * @http://www.liuhaihua.cn
  6.  */
  7. public interface  ClassPathApplicationContext {
  8.     /**
  9.      * instance bean
  10.      */
  11.      void instanceBean();
  12.     /**
  13.      * DI bean
  14.      */
  15.      void injectObject();
  16.     /**
  17.      * read class‘s Annotation info to instance definitionBean
  18.      */
  19.      void readAnnotationCLass();
  20.      /**
  21.       * 初始化
  22.       */
  23.      void init();
  24. }
基于注释的上下文实例化bean类 AnnotationClassPathApplicationContext.java
  1. package com.ajunframework.beans.applicationContext;
  2. import java.util.List;
  3. import com.ajunframework.beans.annotation.Action;
  4. import com.ajunframework.beans.annotation.Dao;
  5. import com.ajunframework.beans.annotation.Service;
  6. import com.ajunframework.beans.definition.BeanDefinition;
  7. import com.ajunframework.beans.definition.BeandefinitionList;
  8. import com.ajunframework.beans.definition.PropertyDefinition;
  9. import com.ajunframework.beans.factory.AnnotationBeanFactory;
  10. import com.ajunframework.beans.scan.ScanClass;
  11. import com.ajunframework.beans.utils.BeanWrapper;
  12. /**
  13.  * 基于注释的上下文实例化bean类
  14.  * @author harries
  15.  * @http://www.liuhaihua.cn
  16.  */
  17. public class AnnotationClassPathApplicationContext implements ClassPathApplicationContext {
  18.     private static AnnotationClassPathApplicationContext cx = new AnnotationClassPathApplicationContext();
  19.     private AnnotationClassPathApplicationContext(){}
  20.     public static AnnotationClassPathApplicationContext getAnnotationClassPathApplicationContext(){
  21.         if(cx!=null){
  22.             return cx;
  23.         }
  24.         return new AnnotationClassPathApplicationContext();
  25.     }
  26.     public void instanceBean() {
  27.          List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
  28.          if(beanDefinitions!=null && !beanDefinitions.isEmpty()){
  29.                 for(BeanDefinition bd:beanDefinitions){
  30.                     if(bd!=null){
  31.                         //初始化bean
  32.                         AnnotationBeanFactory.getBeanFactory().setBean(bd.getId(), bd.getCalssName());
  33.                     }
  34.                 }
  35.         }
  36.     }
  37.     public void injectObject() {
  38.         List<BeanDefinition> beanDefinitions = BeandefinitionList.getBeanDefinitions();
  39.         for(BeanDefinition bd : beanDefinitions){
  40.             Object bean = AnnotationBeanFactory.getBeanFactory().getBean(bd.getId());
  41.             List<PropertyDefinition> pds = bd.getProperties();
  42.             if(pds!=null && !pds.isEmpty()){
  43.                 for(PropertyDefinition pd:pds){
  44.                     String name = pd.getName();
  45.                     String ref = pd.getRef();
  46.                     if(ref!=null && !"".equals(ref)){
  47.                         BeanWrapper b = new BeanWrapper(bean);
  48.                         Object propvalue = AnnotationBeanFactory.getBeanFactory().getBean(name);
  49.                         b.setPropertyValue(name, propvalue);
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.     }
  55.     public void readAnnotationCLass() {
  56.         Class<?> [] classes = ScanClass.getScanPackageClasses();
  57.         if(classes != null && classes.length>0){
  58.             for(Class<?> c:classes){
  59.                 if(c.isAnnotationPresent(Dao.class) || c.isAnnotationPresent(Service.class) || c.isAnnotationPresent(Action.class)){
  60.                     BeandefinitionList.addBeanDefinitionAndSetProperty(c);
  61.                 }
  62.             }
  63.         }
  64.     }
  65.     public  void init(){
  66.         readAnnotationCLass();
  67.         instanceBean();
  68.         injectObject();
  69.     }
  70. }
到目前为止 整个ioc的过程已经完成了,下一节介绍一下这个project中用到的一些工具类
正文到此结束
Loading...