Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Springboot高版本启动报错。需增加配置信息 #452

Closed
bilaisheng opened this issue Dec 13, 2018 · 6 comments
Closed

Springboot高版本启动报错。需增加配置信息 #452

bilaisheng opened this issue Dec 13, 2018 · 6 comments
Assignees
Labels
priority/low Not important, can be finish by new contributors.

Comments

@bilaisheng
Copy link

环境:jdk1.8+ spring boot 2.1.1.RELEASE + Spring boot starter web 2.0.4.RELEASE, nacos 0.2.0-RC1

错误: Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

解决方案:需要增加如下配置,即可解决此问题。如有问题请联系wx:878799579.
image

@zhfish
Copy link
Contributor

zhfish commented Dec 17, 2018

这个未必是nacos的问题。如果是,还是要解决以下的
bean允许覆盖是饮鸠止渴

@HuangDayu
Copy link

不是nacos的问题,我不配置nacos也会报这个错误。

@yanlinly yanlinly added the priority/low Not important, can be finish by new contributors. label Dec 20, 2018
@hxy1991 hxy1991 closed this as completed Dec 26, 2018
@ShZhengqin
Copy link

不一定是版本的问题,我的问题是定义了一个@configuration的类,然后在里面定义了一个@bean,名字叫MyGlobalInterceptor,我发现原来是我在MyGlobalInterceptor上也写了@configuration,就会报这个错误

@glameyzhou
Copy link

glameyzhou commented Jun 13, 2019

这个是SpringBoot版本的问题。
所有的类注册均是在DefaultListenerBeanFactory中完成,其中有一个属性是
private boolean allowBeanDefinitionOverriding = true,无论sb2.0.*还是sb2.1.*都是true。

问题出在sb2.1.*中SpringApplication.prepareContext中会设置DefaultListenerBeanFactory.allowBeanDefinitionOverriding的值。

  • 在sb2.1.*中,如果没有配置spring.main.allow-bean-definition-overriding的值,spring是不允许出现beanName一样的BeanDefinition,直接抛出异常,中断加载启动。

  • 在sb2.0.*中,SpringApplication中并没有设置DefaultListenerBeanFactory.allowBeanDefinitionOverriding的值,因此使用默认的true,允许相同beanName的BeanDefinition注册,不过有一些小规则的判断,有可能做覆盖处理

                BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
		if (existingDefinition != null) {
			if (!isAllowBeanDefinitionOverriding()) {
				throw new BeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition);
			}
			else if (existingDefinition.getRole() < beanDefinition.getRole()) {
				// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
				if (logger.isInfoEnabled()) {
					logger.info("Overriding user-defined bean definition for bean '" + beanName +
							"' with a framework-generated bean definition: replacing [" +
							existingDefinition + "] with [" + beanDefinition + "]");
				}
			}
			else if (!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 + "]");
				}
			}
			this.beanDefinitionMap.put(beanName, beanDefinition);
		}

feignClient如果使用同一个server-id注册多次也会出现类似的情况,官方推荐的做法是手动构建feignClient
create feign clients manually

@tinyking
Copy link

tinyking commented Sep 11, 2019

DefaultListenerBeanFactory

修正下,是这个:DefaultListableBeanFactory

spring-boot:2.1.8.RELEASE中可以看到在SpringApplication中,已经增加了属性字典:

private boolean allowBeanDefinitionOverriding;  // 默认是false

而在spring-boot:1.5.14.RELEASE中,该属性值是不存在。

DefaultListableBeanFactory

/** Whether to allow re-registration of a different definition with the same name. */
private boolean allowBeanDefinitionOverriding = true;

@javaalpha
Copy link

javaalpha commented Aug 5, 2020

关键是加上了这个配置还是提示这个错误,没有生效。
<spring-boot.version>2.1.14.RELEASE</spring-boot.version>
<spring-cloud.version>Greenwich.SR6</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.2.RELEASE</spring-cloud-alibaba.version>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority/low Not important, can be finish by new contributors.
Projects
None yet
Development

No branches or pull requests

10 participants