java - Spring 启动 : Consider defining a bean of type 'com.repository.services.interfacename' in your configuration

标签 java spring spring-mvc spring-boot

我已经部署了一个 spring boot 应用程序,我在其中创建了一个接口(interface)并使用两个类来实现它。

public interface interfacename {
    LinkedHashSet<String> names(String path);
}

实现的类是

@Component
public class class1 implements interfacename {
......
}

@Component
public class class2 implements interfacename {
......
}

现在我尝试使用接口(interface)名称为这两个类创建一个实例,

@Autowired
@Qualifier("class1")
interfacename imp1;

@Autowired
@Qualifier("class2")
interfacename imp2;

是配置类,

@Configuration
public class interfacenameConfig {

    @Bean
    @ConditionalOnProperty(name = "class1", matchIfMissing = true)
    public interfacename class1Service() {
        return new class1();
    }

    @Bean
    @ConditionalOnProperty(name = "stanfordname")
    public interfacename class2Service() {
        return new class2();
    }
}

我的项目结构是,

com.repository
    application.java(@SpringApplcation)
com.repository.controller
    applicationcontroller.java(@RestController)
com.repository.services
    interfacename.java
    interfacenameconfig.java(@configuration)
    class1.java(@component)
    class2.java(@component)

它抛出以下错误 Action :

Consider defining a bean of type 'com.repository.services.interfacename' in your configuration.

请有人指导我解决这个问题。

提前致谢

最佳答案

在您的用法中,您是说您想要 ID/名称分别为 class1class2 的 bean:

@Autowired
@Qualifier("class1")
interfacename imp1;

@Autowired
@Qualifier("class2")
interfacename imp2;

但是在配置中你给了他们不同的名字:

@Configuration
public class interfacenameConfig {

    @Bean
    @ConditionalOnProperty(name = "class1", matchIfMissing = true)
    public interfacename class1Service() {
        return new class1();
    }

    @Bean
    @ConditionalOnProperty(name = "stanfordname")
    public interfacename class2Service() {
        return new class2();
    }
}

即:class1Serviceclass2Service。这些 Id 派生自实例化 bean 的函数的名称

两个可能的修复:

  1. 使用 @Bean("class1")@Bean("class2") 给他们你想要的名字。

  1. 使用限定符中的名称,即:@Qualifier("class1Service")@Qualifier("class2Service")

关于java - Spring 启动 : Consider defining a bean of type 'com.repository.services.interfacename' in your configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46300552/

相关文章:

java - JUnit 应用程序套件测试

Spring 我应该在每个类(class)上使用@DirtiesContext

eclipse - 如何让我的 Maven Junit 测试打印出与 Eclipse JUnit 生成的相同的堆栈跟踪?

java - jsp和html之间的Spring servlet

java - 如何通过 Yarn、Hadoop 提交 Spark scala 作业

java - 为什么我不能在未初始化的局部变量上放置断点?

java - 运行 JBOSS EAP 7 的多个实例

java - Spring MVC 中的@ModelAttribute 是什么?

java - 如何将 List<String> 从 Controller 传递到 <form :select> in Spring 4. 0.0

java - 为什么 GeoTools 找不到 MutablePicoContainer#registerComponentImplementation 方法?