我已经部署了一个 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/名称分别为 class1
和 class2
的 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();
}
}
即:class1Service
和class2Service
。这些 Id 派生自实例化 bean 的函数的名称
两个可能的修复:
- 使用
@Bean("class1")
和@Bean("class2")
给他们你想要的名字。
或
- 使用限定符中的名称,即:
@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/