1. 程式人生 > 其它 >書寫工具類實現手動獲取容器中的bean

書寫工具類實現手動獲取容器中的bean

import java.lang.reflect.Field;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public SpringUtil() {
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }

    public static <T> T getBean(String name, Class<T> cls) throws BeansException {
        return applicationContext.getBean(name, cls);
    }

    public static <T> T getBean(Class<T> cls) throws BeansException {
        return applicationContext.getBean(cls);
    }

    public static void injectBean(Map<String, String> map) {
        String className = (String)map.get("className");
        if (className == null) {
            throw new RuntimeException("map引數缺少className");
        } else {
            Class aClass;
            try {
                aClass = Class.forName(className);
                if (aClass == null) {
                    throw new RuntimeException("className不存在");
                }
            } catch (ClassNotFoundException var10) {
                throw new RuntimeException(var10);
            }

            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(aClass);
            Field[] declaredFields = aClass.getDeclaredFields();
            Field[] var5 = declaredFields;
            int var6 = declaredFields.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                Field declaredField = var5[var7];
                String fieldName = declaredField.getName();
                if (map.get(fieldName) != null) {
                    builder.addPropertyValue(fieldName, map.get(fieldName));
                }
            }

            BeanDefinitionRegistry registry = (BeanDefinitionRegistry)applicationContext;
            String id = (String)map.get("id");
            if (id == null) {
                registry.registerBeanDefinition(className, builder.getBeanDefinition());
            } else {
                registry.registerBeanDefinition(id, builder.getBeanDefinition());
            }
        }
    }
}

使用

  @Test
    public void testAA() throws IOException {
        ApprovalBillService service = SpringUtil.getBean(ApprovalBillService.class);
        Boolean aBoolean = service.approvalBill(null);
        System.out.println(aBoolean);
    }