嘿
我正在将我的网络应用程序从 Spring 3.1 升级到 4.1.8,但遇到问题。我的代码没有改变(只有我的pom.xml)
我的主上下文中有一个配置 bean,如下所示:
@Configuration
public class StorableServiceConfiguration {
...
@Bean
public StorableService<Template, Long> templateService(ITemplateJpaDao dao) {
return new DaoService<Template, Long>(Template.class, dao);
}
}
显然在我的网络应用程序的其他地方,我有这样的声明:
@Autowired
@Qualifier("templateService")
private StorableService<Template, String> templateService;
现在,这一切在 Spring 3.1.1 上运行良好,但在将版本更新到 4.1.8 后,我收到此错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [w.wexpense.service.StorableService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=templateService)}
有人有线索吗?
我在某处读到,Spring 4 中关于上下文:组件扫描关于 @Configuration 注释的行为方式发生了变化,但不记得是什么了。有人知道吗?
谢谢
最佳答案
Spring 4 autowire beans 使用 Java 泛型作为 @Qualifier
的形式.
所以你有一个 Bean @Autowired
与 StorableService<Template, String>
但在你的@Configuration
给你上个课 @Bean
声明StorableService<Template, Long>
.
如果你想要 StorableService<Template, String>
例如,您应该创建另一个 @Bean
在您的@Configuration
类,例如:
@Bean
public StorableService<Template, String> templateService(ITemplateJpaDao dao) {
return new DaoService<Template, String>(Template.class, dao);
}
和 Autowiring 它没有 @Qualifier
注释:
@Autowired
private StorableService<Template, String> templateService;
Spring 4 将完美地注入(inject)它。看看这个博客post来看看 Spring 4 的这个新特性。
关于java - 在 Spring 4 中扫描@Configuration bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34072203/