6

I have this error in spring application(But application Running and working ok):

Could not autowire. There is more than one bean of 'OAuth2ClientContext' type.
Beans:
oauth2ClientContext   (OAuth2RestOperationsConfiguration.class)  
oauth2ClientContext   (OAuth2RestOperationsConfiguration.class)  
oauth2ClientContext   (OAuth2RestOperationsConfiguration.class)

@SpringBootApplication
@RestController
@EnableOAuth2Client
@EnableAuthorizationServer
@Order(6)
public class SocialApplication extends WebSecurityConfigurerAdapter {

  @Autowired
  OAuth2ClientContext oauth2ClientContext;
}

I really can't understand where it coming from and how to fix it. I'm new with this spring stuff, can you point out how to fix it or what should I look for. I'm not creating/annotating beans.

My pom file

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
            <version>1.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>angularjs</artifactId>
            <version>1.4.3</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
Evgenii
  • 447
  • 1
  • 10
  • 26
  • 1
    tried with unique names? – Stultuske May 15 '17 at 16:21
  • I'm not shure I'm understand what you talking about, is it @Qualifier? – Evgenii May 15 '17 at 16:23
  • I think it is your dependencies. What are the project dependencies? – Arash May 15 '17 at 16:42
  • no, it seems like you have three beans defined with the exact same name – Stultuske May 15 '17 at 16:46
  • added my pom. I'm not creating beans. There was inner class(with beans), I thought it could be it, i deleted this class and all other code from SocialApplication class, and still have this error. Also there is no other annotaded classes. And other wierd thing application works fine with this error... – Evgenii May 15 '17 at 16:56

1 Answers1

5

You probably have more then one bean with the same name of OAuth2ClientContext

To fix it you can add a @Qualifier(name='OAuth1') at one of the methods or you can set one of them as @Primary.

Example:

@Autowired
@Qualifier(name = 'service1')
private OAuth2ClientContext oauth;

OR

Where you created your Service you can do this:

@Service
@Primary
public class OAuth2ClientContext{

...
...
..
}
José Mendes
  • 950
  • 2
  • 14
  • 30
  • Problem is - i do not create any of this beans myself. If I understand right, spring doing it for me. Thats why I dont have any Services/Beans/Components to place @Primary annotation and I don't know what i should "Qualify", couse i don't know service/bean/etc name. – Evgenii May 15 '17 at 20:34
  • 3
    I see you have a bunch of Spring-Security libraries in your POM.XML, try to remove some and try, probably, as Spring-Boot creates the beans automatically he is creating 2 beans with the same name due to libraries implementing the same thing. – José Mendes May 16 '17 at 13:34
  • Ye, problem was with all this security libraries. Thank you. – Evgenii May 17 '17 at 18:29