当我将Spring Boot版本从2.0.3.RELEASE
更改为2.1.0.BUILD-SNAPSHOT
时,我的工作代码会停止工作。
有时错误是:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'dataSource', defined in BeanDefinition defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
或...
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-07-16 14:38:18.509 ERROR 604 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'scopedTarget.oauth2ClientContext', defined in class path resource [org/springframework/boot/autoconfigure/security/oauth2/client/OAuth2RestOperationsConfiguration$SessionScopedConfiguration$ClientContextConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/security/oauth2/config/annotation/web/configuration/OAuth2ClientConfiguration$OAuth2ClientContextConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
这两种情况都与重复的Bean或其他有关。
我的POM依赖项是:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
<maven.test.skip>true</maven.test.skip>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
和配置:
server:
error:
include-stacktrace: always
whitelabel:
enabled: false
servlet:
session:
cookie:
name: HYDRASSESSION
port: 36205
management:
endpoints:
web:
exposure:
include: "*"
security:
basic:
enabled: false
oauth2:
client:
clientId: atlas
clientSecret: secret
accessTokenUri: http://myserverip:36202/oauth/token
userAuthorizationUri: http://myserverip:36202/oauth/authorize
resource:
userInfoUri: http://myserverip:36202/user/me
spring:
jpa:
properties:
hibernate:
temp:
use_jdbc_metadata_defaults: false
hibernate:
ddl-auto: validate
application:
name: atlas
datasource:
password: admin
username: postgres
url: jdbc:postgresql://myserverip:36211/atlas?ApplicationName=Atlas
guardiao:
logout:
path: http://myserverip:36202/exit
使用2.0.3.RELEASE
时运行正常,但是尝试登录myserver:36202/oauth/authorize?client_id=
时收到错误消息:浏览器抱怨ERR_TOO_MANY_REDIRECTS
,但控制台中没有错误。无论如何...我的问题是:如何将SpringBoot更新到2.1.0.BUILD-SNAPSHOT?
答案 0 :(得分:21)
因此,作为异常报告,有两个相同类型的bean。从历史上讲,Spring会优先使用另一个Bean。长期以来,这一直很烦人,因为您很难找到其中一个类型完全不同但具有相同ID的第二个bean会使第一个bean消失的错误。
Spring Boot 2现在默认情况下会禁用这种Bean覆盖。您可以通过在application.yml中设置以下属性来重新启用它:
spring.main.allow-bean-definition-overriding: true
这将重新启用以前的行为。它并没有解决Bean被覆盖的根本原因,并且还意味着您将无法获得Bean覆盖错误的好处。升级基础库有望随着时间的流逝清理它。
如其他评论所述,将spring-security-oauth2-autoconfigure
依赖项升级为org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.0.RELEASE
可能会为您解决。
答案 1 :(得分:-1)
此答案对于设置了新项目并且在spring.main.allow-bean-definition-overriding: true
文件中添加application.properties
属性的用户无法解决问题:
检查application.properties
文件所在的文件夹-该文件夹必须直接在目录src/main/resources/
下可用,而不是其他任何位置。