自动装配和注入 bean 的外部 Java 库问题
External Java Library issue with Autowiring and injecting bean
我创建了一个 Spring 由 Maven 管理的启动应用程序。
我正在从我们的 Maven 存储库中检索公司的库。
在这个库中,我们有一个服务接口,没有用'@Service'注释:
public interface MyService {
//...
}
此服务只有一个实现:
public class DefaultMyService implements MyService {
//...
}
此库上下文以旧的 Spring 方式管理(在 applicationContext.xml 文件中)。
我正常阅读,Spring 如果范围内只有一个实现,Boot 能够找到实现。
当我尝试在我的项目上 运行 "spring-boot:run" 时,它将失败并出现以下错误:
No qualifying bean of type 'com.pharmagest.saml.SAMLService'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
我试过了:
- 在配置 class 上添加 @ComponentScan,包括错误的包:
@ComponentScan(basePackages={"com.mycompany.web", "com.mycompany.thelibrary.client.*", "com.mycompany.thelibrary.services.*"})
- 要在applicationContext.xml中添加bean定义(如果我添加接口它告诉我它可以定义它,所以我听说Spring如果只有一个就可以找到默认实现? )
- 在项目选项 "runtime" 添加库
- 不通过 maven 添加库作为外部资源
在所有情况下,我只能构建 maven,但不能 运行 项目。
你有什么建议可以帮助我吗?谢谢!
- 不会工作,因为
DefaultMyService
没有 @Component
(或 @Service
)注释将不会被检测到。
- Bean 定义必须是具体实例,因此使用
DefaultMyService
而不是接口。 Spring不会为你检测任何东西你的理解是错误的
- 和 4. 不会更改任何内容,仅在没有适当的 1. 或 2. 的情况下添加依赖项将不会执行任何操作。
只需在您的配置中添加一个 @Bean
@Bean
public DefaultMyService myService() {
return new DefaultMyService();
}
或导入其他库 applicatiponContext.xml
,这可能是您应该做的。
@ImportResource("classpath:/applicationContext.xml")
将此添加到 @SpringBootApplication
旁边。
我创建了一个 Spring 由 Maven 管理的启动应用程序。 我正在从我们的 Maven 存储库中检索公司的库。
在这个库中,我们有一个服务接口,没有用'@Service'注释:
public interface MyService {
//...
}
此服务只有一个实现:
public class DefaultMyService implements MyService {
//...
}
此库上下文以旧的 Spring 方式管理(在 applicationContext.xml 文件中)。 我正常阅读,Spring 如果范围内只有一个实现,Boot 能够找到实现。
当我尝试在我的项目上 运行 "spring-boot:run" 时,它将失败并出现以下错误:
No qualifying bean of type 'com.pharmagest.saml.SAMLService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我试过了:
- 在配置 class 上添加 @ComponentScan,包括错误的包:
@ComponentScan(basePackages={"com.mycompany.web", "com.mycompany.thelibrary.client.*", "com.mycompany.thelibrary.services.*"})
- 要在applicationContext.xml中添加bean定义(如果我添加接口它告诉我它可以定义它,所以我听说Spring如果只有一个就可以找到默认实现? )
- 在项目选项 "runtime" 添加库
- 不通过 maven 添加库作为外部资源
在所有情况下,我只能构建 maven,但不能 运行 项目。 你有什么建议可以帮助我吗?谢谢!
- 不会工作,因为
DefaultMyService
没有@Component
(或@Service
)注释将不会被检测到。 - Bean 定义必须是具体实例,因此使用
DefaultMyService
而不是接口。 Spring不会为你检测任何东西你的理解是错误的 - 和 4. 不会更改任何内容,仅在没有适当的 1. 或 2. 的情况下添加依赖项将不会执行任何操作。
只需在您的配置中添加一个 @Bean
@Bean
public DefaultMyService myService() {
return new DefaultMyService();
}
或导入其他库 applicatiponContext.xml
,这可能是您应该做的。
@ImportResource("classpath:/applicationContext.xml")
将此添加到 @SpringBootApplication
旁边。