问题描述
最近在学习spring cloud sleuth
过程中,遇到了一个问题
Thebean'characterEncodingFilter', definedinclasspathresource [zipkin/autoconfigure/ui/ZipkinUiAutoConfiguration.class], couldnotberegistered. Abeanwiththatnamehasalreadybeendefinedinclasspathresource [org/springframework/boot/autoconfigure/web/servlet/HttpEncodingAutoConfiguration.class] andoverridingisdisabled. Action: Considerrenamingoneofthebeansorenablingoverridingbysettingspring.main.allow-bean-definition-overriding=true
从错误信息中可以看到,characterEncodingFilter这个bean被定义了两次,ZipkinUiAutoConfiguration和HttpEncodingAutoConfiguration都有定义。在大型项目开发过程中,这种情况并不少见。毕竟各个不同的组件都是独立开发的,集成到一起后总会遇到各种惊喜。spring.main.allow-bean-definition-overriding=true就是解决bean重复定义的。设置为true时,后定义的bean会覆盖之前定义的相同名称的bean。
问题分析
上面已经看到,spring.main.allow-bean-definition-overriding设置为true,表示后发现的bean会覆盖之前相同名称的bean。
问题就出在spring初始化时bean工厂加载bean的时候。我们来看一下DefaultListableBeanFactory代码:
/** 是否允许使用相同名称重新注册不同的bean实现. 默认是允许*/privatebooleanallowBeanDefinitionOverriding=true; /*** Set whether it should be allowed to override bean definitions by registering* a different definition with the same name, automatically replacing the former.* If not, an exception will be thrown. This also applies to overriding aliases.* <p>Default is "true".【这里明确说明了默认是true】* @see #registerBeanDefinition*/publicbooleanisAllowBeanDefinitionOverriding() { returnthis.allowBeanDefinitionOverriding; } publicvoidregisterBeanDefinition(StringbeanName, BeanDefinitionbeanDefinition) throwsBeanDefinitionStoreException { Assert.hasText(beanName, "Bean name must not be empty"); Assert.notNull(beanDefinition, "BeanDefinition must not be null"); if (beanDefinitioninstanceofAbstractBeanDefinition) { try { ((AbstractBeanDefinition) beanDefinition).validate(); } catch (BeanDefinitionValidationExceptionex) { thrownewBeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName, "Validation of bean definition failed", ex); } } //bean加载到spring的工程中后,会存储在beanDefinitionMap中,key是bean的名称。BeanDefinitionexistingDefinition=this.beanDefinitionMap.get(beanName); if (existingDefinition!=null) {//不为空,说明相同名称的bean已经存在了if (!isAllowBeanDefinitionOverriding()) {//如果不允许相同名称的bean存在,则直接抛出异常thrownewBeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition); } elseif (existingDefinition.getRole() <beanDefinition.getRole()) { // e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTUREif (logger.isInfoEnabled()) { logger.info("Overriding user-defined bean definition for bean '"+beanName+"' with a framework-generated bean definition: replacing ["+existingDefinition+"] with ["+beanDefinition+"]"); } } elseif (!beanDefinition.equals(existingDefinition)) { if (logger.isDebugEnabled()) { logger.debug("Overriding bean definition for bean '"+beanName+"' with a different definition: replacing ["+existingDefinition+"] with ["+beanDefinition+"]"); } } else { if (logger.isTraceEnabled()) { logger.trace("Overriding bean definition for bean '"+beanName+"' with an equivalent definition: replacing ["+existingDefinition+"] with ["+beanDefinition+"]"); } } //可见,上面allowBeanDefinitionOverriding =true时,只是记录了一些日志,然后后来发现的这个bean,会覆盖之前老的bean。this.beanDefinitionMap.put(beanName, beanDefinition); } else { if (hasBeanCreationStarted()) { // Cannot modify startup-time collection elements anymore (for stable iteration)synchronized (this.beanDefinitionMap) { this.beanDefinitionMap.put(beanName, beanDefinition); List<String>updatedDefinitions=newArrayList<>(this.beanDefinitionNames.size() +1); updatedDefinitions.addAll(this.beanDefinitionNames); updatedDefinitions.add(beanName); this.beanDefinitionNames=updatedDefinitions; if (this.manualSingletonNames.contains(beanName)) { Set<String>updatedSingletons=newLinkedHashSet<>(this.manualSingletonNames); updatedSingletons.remove(beanName); this.manualSingletonNames=updatedSingletons; } } } else { // Still in startup registration phasethis.beanDefinitionMap.put(beanName, beanDefinition); this.beanDefinitionNames.add(beanName); this.manualSingletonNames.remove(beanName); } this.frozenBeanDefinitionNames=null; } if (existingDefinition!=null||containsSingleton(beanName)) { resetBeanDefinition(beanName); } }
具体可以看上面代码中添加的注释。
可以看一下BeanDefinitionOverrideException
的定义,我们上面抛出的异常,就是这里抛出的。
publicBeanDefinitionOverrideException( StringbeanName, BeanDefinitionbeanDefinition, BeanDefinitionexistingDefinition) { super(beanDefinition.getResourceDescription(), beanName, "Cannot register bean definition ["+beanDefinition+"] for bean '"+beanName+"': There is already ["+existingDefinition+"] bound."); this.beanDefinition=beanDefinition; this.existingDefinition=existingDefinition; }
实际错误时提示bean重复,不过这里spring又对其进行了封装,最终打印出来的结果就是本文开头的错误输出,并且提示了我们要配置spring.main.allow-bean-definition-overriding=true。
可能有的人会问了,上面代码中默认就是true啊,为什么还要我手动配置?
原因就是上面贴出来的是spring的代码,而springboot对这个参数又进行了二次封装,springboot中的allowBeanDefinitionOverriding是没有初始化默认值的,我们知道,java中的boolean类型不初始化时是false。
springboot中源代码:
//没有默认初始化就是falseprivatebooleanallowBeanDefinitionOverriding; /*** Sets if bean definition overriding, by registering a definition with the same name* as an existing definition, should be allowed. Defaults to {@code false}.【这里写的很明白了,默认是false】* @param allowBeanDefinitionOverriding if overriding is allowed* @since 2.1* @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding(boolean)*/publicvoidsetAllowBeanDefinitionOverriding(booleanallowBeanDefinitionOverriding) { this.allowBeanDefinitionOverriding=allowBeanDefinitionOverriding; } privatevoidprepareContext(ConfigurableApplicationContextcontext, ConfigurableEnvironmentenvironment, SpringApplicationRunListenerslisteners, ApplicationArgumentsapplicationArguments, BannerprintedBanner) { context.setEnvironment(environment); postProcessApplicationContext(context); applyInitializers(context); listeners.contextPrepared(context); if (this.logStartupInfo) { logStartupInfo(context.getParent() ==null); logStartupProfileInfo(context); } // Add boot specific singleton beansConfigurableListableBeanFactorybeanFactory=context.getBeanFactory(); beanFactory.registerSingleton("springApplicationArguments", applicationArguments); if (printedBanner!=null) { beanFactory.registerSingleton("springBootBanner", printedBanner); } if (beanFactoryinstanceofDefaultListableBeanFactory) { //**在此处给bean工程设置属性** ((DefaultListableBeanFactory) beanFactory) .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } // Load the sourcesSet<Object>sources=getAllSources(); Assert.notEmpty(sources, "Sources must not be empty"); load(context, sources.toArray(newObject[0])); listeners.contextLoaded(context); }
到底 allowBeanDefinitionOverriding 应该设置 true 还是 false?
上面代码中可以看到,spring中默认是true,也就是默认支持名称相同的bean的覆盖。而springboot中的默认值是false,也就是不支持名称相同的bean被覆盖。
那么我们自己应该如何选择呢?
这里笔者认为默认不覆盖比较好。
因为还是推荐一个系统中不要存在名称相同的bean,否则后者覆盖前者,多人分工合作的时候,难以避免某些bean被覆盖,会出现很多诡异的问题 ,甚至会带来线上真实的业务损失。
Bean的名称不相同,依据具体的业务给bean起名字。这样不但可以解决bean名称重复的问题,还可以大大提高程序的可读性与可维护性。
只有当集成了第三方的库,不同库直接由于是多个团队开发的,甚至这些团队属于不同的国家,有可能会出现bean名称相同的情况。这种情况就需要根据实际需求来设置allowBeanDefinitionOverriding的值了。