繁体   English   中英

在Spring 4中扫描@Configuration bean

[英]Scanning @Configuration beans in Spring 4

海兰

我正在将Web应用程序从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);
    }
}

显然,在我的Web应用程序中的其他地方,我有以下语句:

@Autowired
@Qualifier("templateService")
private StorableService<Template, String> templateService;

现在,这一切在Spring 3.1.1上都可以正常工作,但是在将版本更新为4.1.8之后,出现了此错误:

由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[w.wexpense.service.StorableService]的合格Bean作为依赖项:期望至少有1个Bean可以作为此依赖项的自动装配候选。 依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = templateService)}

有人知道了吗?

我在某个地方读到,在Spring 4中,有关context:component-scan在@Configuration注释方面的行为有所更改,但不记得是什么。 有人知道吗?

谢谢

使用Java泛型作为@Qualifier形式的Spring 4 autowire bean。

因此,您有一个带有StorableService<Template, String>的Bean @Autowired ,但在您的@Configuration类中,您的@Bean声明了StorableService<Template, Long>

如果要使用StorableService<Template, String>实例,则应在@Configuration类中创建另一个@Bean ,例如:

@Bean 
public StorableService<Template, String> templateService(ITemplateJpaDao dao) {
    return new DaoService<Template, String>(Template.class, dao);
}

并在没有@Qualifier批注的情况下自动装配

@Autowired
private StorableService<Template, String> templateService;

Spring 4将完美地注入它。 查看此博客文章以查看Spring 4的这一新功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM