[英]spring boot scanning and injecting external non-spring beans
这需要什么,或者Spring可能扫描并注入非Spring注释的类? 例如。
resource.jar
com.project.resource.ResourceInterface
com.project.resource.StandardResource <-- concrete implementation
@Singleton <--- Standard CDI annotation
public class StandardResource implements ResourceInterface{
@Override
public void something(){}
}
现在,我们有一个依赖于resource.jar
的spring boot应用程序。
com.project.resource.SpringApp
@SpringBootApplication(scanBasePackages = {"com.project"})
@EnableAutoConfiguration
public class SpringApp{
... initializer
@Inject
private ResourceInterface resourceService; <--- this is not found
}
这应该可以立即使用吗? 这有可能吗? 我正在使用Spring Boot 2.0.0.RELEASE
。 我收到以下错误:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'MainController': Unsatisfied dependency expressed through field 'resourceService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.project.resource.ResourceInterface' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject()}
谢谢
对于Spring框架@Singleton
没有任何意义,因此即使组件扫描选择了类,也将被忽略。 为了让Spring识别您的班级,您可以:
@Bean
在com.project.resource中创建一个配置类 ResourceInterface
并将其实例化为StandardResource
。 之后,您的Spring Boot应用将正常运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.