动态注入bean

/**
 * bean动态注入IOC容器
 */
@Component
public class BeanRegistry {

    private static final Logger logger = LoggerFactory.getLogger(BeanRegistry.class);

    @Autowired
    private SpringBeanTools springBeanTools;

    /**
     * 向Spring容器中注入bean(构造器注入)
     *
     * @param beanName        bean名称
     * @param beanClass       bean Class对象
     * @param constructorArgs 参数
     * @param 
     */
    public  void registerBean(String beanName, Class beanClass, Object... constructorArgs) {
        if (null == beanClass) {
            if (logger.isDebugEnabled()) {
               
            }
            return;
        }

        //构建BeanDefinitionBuilder
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(beanClass);
        for (Object obj : constructorArgs) {
            builder.addConstructorArgValue(obj);
        }

        //从builder中获取到BeanDefinition对象
        BeanDefinition beanDefinition = builder.getRawBeanDefinition();

        //获取spring容器中的IOC容器
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) springBeanTools.getApplicationContext().getAutowireCapableBeanFactory();

        //向IOC容器中注入bean对象
        defaultListableBeanFactory.registerBeanDefinition(beanName, beanDefinition);
        //return defaultListableBeanFactory.getBean(beanName, beanClass);
    }

}

/**
 * 获取spring上下文
 * huangjq
 */
@Component
public class SpringBeanTools implements ApplicationContextAware {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 获取指定的bean对象
     *
     * @param name  bean名称
     * @param clazz 类class对象
     * @param 
     * @return Bean对象
     */
    @SuppressWarnings("unchecked")
    public  T getBean(String name, Class clazz) {
        //applicationContext非空
        Assert.isTrue((applicationContext != null), "应用上下文不能为空");
        Object bean = applicationContext.getBean(name);
        if (bean == null) {
            return null;
        }
        return (T)bean;
    }

    /**
     * 获取bean对象实例
     *
     * @param clazz 类class对象
     * @param 
     * @return Bean对象
     */
    public  T getBean(Class clazz) {
        return applicationContext.getBean(clazz);
    }

    /**
     * 获取bean对象实例
     *
     * @param name 实例名称
     * @return
     */
    public Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    /**
     * 获取 applicationContext 的值
     *
     * @return applicationContext
     */
    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

}

/**
 * bean 注入
 */
public class ManualRegistBeanUtil {

    /**
    * 主动向Spring容器中注册bean
    *
    * @param applicationContext Spring容器
    * @param name               BeanName
    * @param clazz              注册的bean的类性
    * @param args               构造方法的必要参数,顺序和类型要求和clazz中定义的一致
    * @param 
    * @return 返回注册到容器中的bean对象
    */
   @SuppressWarnings("unchecked")
   public static  T registerBean(ConfigurableApplicationContext applicationContext, String name, Class clazz, Object[] args) {
       if(applicationContext.containsBean(name)) {
           Object bean = applicationContext.getBean(name);
           if (bean.getClass().isAssignableFrom(clazz)) {
               return (T) bean;
           } else {
               throw new RuntimeException("BeanName 重复 " + name);
           }
       }

       BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
       for (Object arg : args) {
           beanDefinitionBuilder.addConstructorArgValue(arg);
       }
       BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();

       BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) applicationContext.getBeanFactory();
       beanFactory.registerBeanDefinition(name, beanDefinition);
       return applicationContext.getBean(name, clazz);
   }
}



 

你可能感兴趣的:(Java)