[英]Scanning @Configuration beans in Spring 4
Hy 海兰
I am upgrading my web app from Spring 3.1 to 4.1.8 but having issues. 我正在将Web应用程序从Spring 3.1升级到4.1.8,但是有问题。 My code has not changed (only my pom.xml) 我的代码没有改变(只有我的pom.xml)
I have a configuration bean in my main context that looks like: 我的主要上下文中有一个配置Bean,如下所示:
@Configuration
public class StorableServiceConfiguration {
...
@Bean
public StorableService<Template, Long> templateService(ITemplateJpaDao dao) {
return new DaoService<Template, Long>(Template.class, dao);
}
}
And obviously somewhere else in my web app, I have this statement: 显然,在我的Web应用程序中的其他地方,我有以下语句:
@Autowired
@Qualifier("templateService")
private StorableService<Template, String> templateService;
Now this all worked fine with Spring 3.1.1 but after updating the version to 4.1.8, I am getting this error: 现在,这一切在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. 由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[w.wexpense.service.StorableService]的合格Bean作为依赖项:期望至少有1个Bean可以作为此依赖项的自动装配候选。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=templateService)} 依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = templateService)}
Anybody got a clue? 有人知道了吗?
I read somewhere that there was a change in Spring 4 on how the context:component-scan behave regarding the @Configuration annotation but can't remember what. 我在某个地方读到,在Spring 4中,有关context:component-scan在@Configuration注释方面的行为有所更改,但不记得是什么。 Is anybody aware of that? 有人知道吗?
Thanks 谢谢
Spring 4 autowire beans using Java generics as form of @Qualifier
. 使用Java泛型作为@Qualifier
形式的Spring 4 autowire bean。
So you have a Bean @Autowired
with StorableService<Template, String>
but in your @Configuration
class your @Bean
declares StorableService<Template, Long>
. 因此,您有一个带有StorableService<Template, String>
的Bean @Autowired
,但在您的@Configuration
类中,您的@Bean
声明了StorableService<Template, Long>
。
If you want a StorableService<Template, String>
instance you should create another @Bean
at your @Configuration
class, for example: 如果要使用StorableService<Template, String>
实例,则应在@Configuration
类中创建另一个@Bean
,例如:
@Bean
public StorableService<Template, String> templateService(ITemplateJpaDao dao) {
return new DaoService<Template, String>(Template.class, dao);
}
and autowire it without the @Qualifier
annotation: 并在没有@Qualifier
批注的情况下自动装配 :
@Autowired
private StorableService<Template, String> templateService;
Spring 4 will inject it perfectly. Spring 4将完美地注入它。 Look at this blog post to see this new feature of Spring 4. 查看此博客文章以查看Spring 4的这一新功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.