spring bean注册

org.bamboo.springx.BeanFactory

 

   protected void registerBean(String idOrName, Class beanClass, boolean isSingleton) {
        DefaultListableBeanFactory beanFactory = getBeanFactory();
        if (!beanFactory.containsBean(idOrName)) {

            RootBeanDefinition beanDefinition = new RootBeanDefinition();
            beanDefinition.setBeanClassName(beanClass.getName());
            beanDefinition.setSingleton(isSingleton); // 默认为true, 需要增加可配置性
            beanDefinition.setAutowireCandidate(true);
            // He Kun, 2009-9-3, 改为byName,避免一个接口多个实现类都是注册 bean 时发生 type 冲突。
            beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);

            beanFactory.registerBeanDefinition(idOrName, beanDefinition);

            if (log.isDebugEnabled()) log.debug("Register bean: " + idOrName + " singleton?:" + isSingleton);
        }

    }

 

 public Object getBean(String beanID) {

        if (containsBean(beanID)) return context.getBean(beanID);
        else {
            throw new BeanNotFoundException(beanID);
        }
    }

你可能感兴趣的:(spring,bean)