它需要什么,或者甚至可以对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(){}
}
现在假设我有一个Spring启动应用程序,它取决于resource.jar
。
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()}
由于
答案 0 :(得分:1)
对于Spring框架@Singleton
没有意义,因此即使通过组件扫描获取类,它也会被忽略。为了让Spring认出你的课程,你可以:
@Bean
ResourceInterface
并将其实例化为StandardResource
。 之后,您的春季启动应用程序将正常运行