无法自动装配,有多个bean错误

Evg*_*nii 6 java spring oauth oauth-2.0 spring-boot

我在spring应用程序中有这个错误(但是应用程序运行并正常工作):

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;
}
Run Code Online (Sandbox Code Playgroud)

我真的无法理解它来自何处以及如何解决它.我对这个春天的东西很新,你能指出如何解决它或我应该寻找什么.我不是在创建/注释bean.

我的pom文件

    <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>
Run Code Online (Sandbox Code Playgroud)

Jos*_*des 6

您可能有多个具有相同名称的bean OAuth2ClientContext

要修复它,您可以@Qualifier(name='OAuth1')在其中一个方法中添加,也可以将其中一个设置为@Primary.

例:

@Autowired
@Qualifier(name = 'service1')
private OAuth2ClientContext oauth;
Run Code Online (Sandbox Code Playgroud)

要么

你创建的地方Service你可以这样做:

@Service
@Primary
public class OAuth2ClientContext{

...
...
..
}
Run Code Online (Sandbox Code Playgroud)

  • 我看到你的 POM.XML 中有一堆 Spring-Security 库,尝试删除一些并尝试,可能是因为 Spring-Boot 自动创建了 bean,由于库实现了相同的东西,他正在创建 2 个同名的 bean . (3认同)