package org.smart4j.framework.helper;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.smart4j.framework.annotation.Inject;
import org.smart4j.framework.util.ReflectionUtil;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * ע
 * @author zhangrh
 * @date 2016/11/21 0021
 */
public final class IocHelper {

    static {
        //ȡеBeanBeanʵ֮ӳϵ(Bean Map)
        Map<Class<?>,Object> beanMap = BeanHelper.getBeanMap();
        if(MapUtils.isNotEmpty(beanMap)){
            //Bean Map
            for(Map.Entry<Class<?>,Object> beanEntry:beanMap.entrySet()){
                //BeanMapлȡBeanBeanʵ
                Class<?> beanClass = beanEntry.getKey();
                Object beanInstance = beanEntry.getValue();
                //ȡBeanඨгԱ( Bean Field)
                Field[] beanFields = beanClass.getDeclaredFields();
                if(ArrayUtils.isNotEmpty(beanFields)){
                    //beanFields
                    for(Field beanField:beanFields){
                        //жϵǰBean Field ǷInjectע
                        if(beanField.isAnnotationPresent(Inject.class)){
                            //Bean Map ȡBean FieldӦʵ
                            Class<?> beanFieldClass = beanField.getType();
                            Object beanFieldInstance = beanMap.get(beanFieldClass);
                            if(beanFieldInstance != null){
                                //ͨʼBeanFieldֵ
                                ReflectionUtil.setField(beanInstance,beanField,beanFieldInstance);
                            }
                        }
                    }

                }
            }

        }

    }
}

@@ -286,17 +286,23 @@
 %0A/**%0A * 
-%D7%A2
+%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E5%8A%A9%E6%89%8B%E7%B1%BB
 %0A * @aut
@@ -401,23 +401,35 @@
   //
-%C8%A1%D0%B5
+%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E7%9A%84
 Bean
+%E7%B1%BB%E5%92%8C
 Bean
-%CA%B5%D6%AE%D3%B3%CF%B5(
+%E5%AE%9E%E4%BE%8B%E4%B9%8B%E9%97%B4%E7%9A%84%E6%98%A0%E5%B0%84%E5%85%B3%E7%B3%BB(%E7%AE%80%E7%A7%B0
 Bean
@@ -546,32 +546,34 @@
 %7B%0A            //
+%E9%81%8D%E5%8E%86
 Bean Map%0A       
@@ -661,26 +661,31 @@
   //
+%E4%BB%8E
 BeanMap
-%D0%BB%C8%A1
+%E4%B8%AD%E8%8E%B7%E5%8F%96
 Bean
+%E7%B1%BB%E4%B8%8E
 Bean
-%CA%B5
+%E5%AE%9E%E4%BE%8B
 %0A   
@@ -820,17 +820,27 @@
   //
-%C8%A1
+%E8%8E%B7%E5%8F%96
 Bean
-%E0%B6%A8%D0%B3%D4%B1(
+%E7%B1%BB%E5%AE%9A%E4%B9%89%E7%9A%84%E6%89%80%E6%9C%89%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F(%E7%AE%80%E7%A7%B0
  Bea
@@ -993,16 +993,18 @@
       //
+%E9%81%8D%E5%8E%86
 beanFiel
@@ -1089,11 +1089,12 @@
   //
-%D0%B6%CF%B5%C7%B0
+%E5%88%A4%E6%96%AD%E5%BD%93%E5%89%8D
 Bean
@@ -1104,16 +1104,20 @@
 eld 
-%C7%B7
+%E6%98%AF%E5%90%A6%E5%B8%A6%E6%9C%89
 Inject
-%D7%A2
+%E6%B3%A8%E8%A7%A3
 %0A   
@@ -1216,16 +1216,17 @@
       //
+%E5%9C%A8
 Bean Map
@@ -1230,9 +1230,10 @@
 Map 
-%C8%A1
+%E8%8E%B7%E5%8F%96
 Bean
@@ -1242,10 +1242,13 @@
 ield
-%D3%A6%CA%B5
+%E5%AF%B9%E5%BA%94%E7%9A%84%E5%AE%9E%E4%BE%8B
 %0A   
@@ -1500,10 +1500,15 @@
   //
-%CD%A8%CA%BC
+%E9%80%9A%E8%BF%87%E5%8F%8D%E5%B0%84%E5%88%9D%E5%A7%8B%E5%8C%96
 Bean
@@ -1516,9 +1516,10 @@
 ield
-%D6%B5
+%E7%9A%84%E5%80%BC
 %0A   

UTF-8