spring中如何修改allowBeanDefinitionOverriding

首先得弄清楚这两个参数的意思:

allowBeanDefinitionOverriding

运行beanDefinition覆盖,默认情况下,如果beanDefinitionMap有重复的key存在时就会抛异常,就是由这个参数决定的。如果把这个参数设置为true,那么就允许相同key情况下beanDefinition实例的覆盖。源码请看:

当spring解析<bean>标签,并且封装成beanDefinition对象时会把该对象缓存起来,具体源码如下:

DefaultListableBeanFactory类中
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
      throws BeanDefinitionStoreException {

   Assert.hasText(beanName, "Bean name must not be empty");
   Assert.notNull(beanDefinition, "BeanDefinition must not be null");

   if (beanDefinition instanceof AbstractBeanDefinition) {
      try {
         ((AbstractBeanDefinition) beanDefinition).validate();
      }
      catch (BeanDefinitionValidationException ex) {
         throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
               "Validation of bean definition failed", ex);
      }
   }

   synchronized (this.beanDefinitionMap) {
      BeanDefinition oldBeanDefinition = this.beanDefinitionMap.get(beanName);
      if (oldBeanDefinition != null) {
         if (!this.allowBeanDefinitionOverriding) {
            throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
                  "Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
                  "': There is already [" + oldBeanDefinition + "] bound.");
         }
         else if (oldBeanDefinition.getRole() < beanDefinition.getRole()) {
            // e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
            if (this.logger.isWarnEnabled()) {
               this.logger.warn("Overriding user-defined bean definition for bean '" + beanName +
                     " with a framework-generated bean definition ': replacing [" +
                     oldBeanDefinition + "] with [" + beanDefinition + "]");
            }
         }
         else {
            if (this.logger.isInfoEnabled()) {
               this.logger.info("Overriding bean definition for bean '" + beanName +
                     "': replacing [" + oldBeanDefinition + "] with [" + beanDefinition + "]");
            }
         }
      }
      else {
         this.beanDefinitionNames.add(beanName);
         this.frozenBeanDefinitionNames = null;
      }
      this.beanDefinitionMap.put(beanName, beanDefinition);
   }

   resetBeanDefinition(beanName);
}

其中

if (!this.allowBeanDefinitionOverriding) { throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName, “Cannot register bean definition [” + beanDefinition + “] for bean ‘” + beanName + “‘: There is already [” + oldBeanDefinition + “] bound.”); }

这个if判断就是根据allowBeanDefinitionOverriding来判断是否允许相同key的覆盖的。

 

那么如何修改这个属性呢??

看源码

类AbstractRefreshableApplicationContext

protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
   if (this.allowBeanDefinitionOverriding != null) {
      beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
   }
   if (this.allowCircularReferences != null) {
      beanFactory.setAllowCircularReferences(this.allowCircularReferences);
   }
}
public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) {
   this.allowBeanDefinitionOverriding = allowBeanDefinitionOverriding;
}

有一个public的set方法,也就是说这个方法是可以被调用的,只要拿到ApplicationContext上下文对象就可以调用这个方法进行属性的设置

applicationContext.setAllowBeanDefinitionOverriding(true);就可以了

那我问他又来了,如果能拿到spring上下文对象了,如何此时再设置这个属性,那么这个属性肯定是没有生效的,如何让这个属性生效呢???

applicationContext.refresh();  就是这么简单,调用上下文对象的refresh()方法就 可以了 

 


版权声明:本文为luoyang_java原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。