33

I have a working code that stops to work when I change Spring Boot version from 2.0.3.RELEASE to 2.1.0.BUILD-SNAPSHOT.

Sometimes the error is:

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

or ...

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

Both cases it is related to duplicated bean os something else.

My POM dependencies are:

    <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>

and the config:

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

It run fine when using 2.0.3.RELEASE but I'm receiving an error when try to login myserver:36202/oauth/authorize?client_id=: by browser complains ERR_TOO_MANY_REDIRECTS but I have no errors in console. Anyway... my question is: how can I update my SpringBoot to 2.1.0.BUILD-SNAPSHOT ?

Magno C
  • 1,922
  • 4
  • 28
  • 53
  • 1
    Why would you want to use 2.1? That isn't final until about 2 to 3 months? Instead fix the actual issue, that won't be solved by just upgrading Spring Boot (and everything else to nightly builds). – M. Deinum Jul 16 '18 at 18:01
  • You're right. I should not do this. Actualy... the real issue was the `clientSecret: secret` .... sorry. – Magno C Jul 16 '18 at 18:29
  • By the way... thinking ahead I'm afraid to get this issue anyway in the future when I need to upgrade. Do you know what was changed? – Magno C Jul 17 '18 at 11:39
  • 3
    This is still a problem now that 2.1 is released today. Did you find a solution yet? – checketts Oct 30 '18 at 18:50
  • 1
    Was able to resolve the first issue related to datasource by upgrading the Spring Cloud version to Greenwich.M1, but the second exception still persists. – Munish Chandel Oct 31 '18 at 03:05
  • 2
    a workaround solution is add below into application.yml `spring.main.allow-bean-definition-overriding: true` – Aura Oct 31 '18 at 12:28
  • 2
    scopedTarget.oauth2ClientContext error is gone after updating this dependency compile group: 'org.springframework.security.oauth.boot', name: 'spring-security-oauth2-autoconfigure', version: '2.1.0.RELEASE' – Munish Chandel Nov 02 '18 at 02:28
  • @checketts, have the same issue. Did you find a solution? – StasKolodyuk Nov 02 '18 at 09:43
  • Faced with the same problem today. For me it was caused by presence of Spring cloud dependencies brought by OpenFeign starter. – Kirill Nov 22 '18 at 11:46
  • Golden rule - never rely on snapshot builds. – vikingsteve Nov 23 '18 at 13:49
  • @Derp, then [what was the fix please?](https://xkcd.com/979/) –  Jun 07 '19 at 19:46
  • @JarrodRoberson just checked my pom: I used `` and pinned Spring Cloud version to `Finchley.SR2` – Kirill Jun 08 '19 at 08:45

2 Answers2

51

So as the exception reports, there are two beans of the same type. Historically Spring would override one bean with the other. That has long been an annoyance since you could get hard to find bugs where a second bean with a completely different type, but with the same bean ID would make your first bean vanish.

Spring Boot 2 now disables that sort of bean overriding by default. You can re-enable it by setting the following property in your application.yml:

spring.main.allow-bean-definition-overriding: true

This re-enables the previous behavior. It doesn't address the root cause that beans are being overridden though and also mean you won't get the benefit of bean override errors. Upgrading the underlying libraries will hopefully clean this up over time.

As noted on other comments, upgrading the spring-security-oauth2-autoconfigure dependency to org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.0.RELEASE may solve it for you.

checketts
  • 14,167
  • 10
  • 53
  • 82
  • What version the Initialzr will give us? – Magno C Nov 07 '18 at 15:15
  • This one Solved "upgrading the spring-security-oauth2-autoconfigure dependency to org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.0.RELEASE " – Arjun Shetty Aug 21 '19 at 16:52
  • I have added the above property and it fixed my issue, thanks – LearnToCode Jul 08 '21 at 14:59
  • Is there any significant change between 2.3.0 and 2.5.5? I got the error after upgrade, but cannot find whether there was something changed. – Peters_ Nov 18 '21 at 08:54
  • @Peters_ New beans being introduced that clash with yours would trigger it. Ask a new question with the details. – checketts Nov 19 '21 at 21:12
0

This answer might be helpful for those who have setup a new project and adding the spring.main.allow-bean-definition-overriding: true property in application.properties file is not fixing the issue:

Check the folder where your application.properties file is present -- it needs to be available directly under the directory: src/main/resources/ and not anywhere else.

Dimi Ansari
  • 310
  • 3
  • 16