我碰到一个tutorial这似乎是适合我的用例,并试图实现它。我失败了,但不知道为什么。于是,我就找到类似的代码另一个例子,看着书“春行动,第四版由Craig墙”Spring @ComponentScan有什么可能的原因是无法自动创建一个由@Repository指定的类
的书描述了在300页相同的基本方法。首先定义一个JdbcTemplate Bean。
@Bean
NamedParameterJdbcTemplate jdbcTemplate(DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}
然后,库实现一个接口
@Repository
public class CustomRepositoryImpl implements CustomRepository {
private final NamedParameterJdbcOperations jdbcOperations;
private static final String TEST_STRING = "";
@Autowired
public CustomRepositoryImpl(NamedParameterJdbcOperations jdbcOperations) {
this.jdbcOperations = jdbcOperations;
}
所以我不喜欢在书中提出的例子,写了一个测试,但得到的错误消息
错误创建名称为豆'de.myproject.config.SpringJPAPerformanceConfigTest':通过字段'abc'表示的不满足的依赖性;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的类型为'de.myproject.CustomRepository'的合格bean:预计至少有1个bean有资格作为autowire候选者。依赖注解:{@ org.springframework.beans.factory.annotation.Autowired(必需=真)}
据我了解的书和教程描述,存储库应该由组件扫描被识别为一个Bean定义。
为了测试这个,我创建了一个上下文并询问了所有注册的Bean。
AnnotationConfigApplicationContext
context = new AnnotationConfigApplicationContext();
context.getBeanDefinitionNames()
假设我的存储库不在其中。因此,我只为了测试目的而增加了我的项目中的搜索范围,并将其设置为基本包。除储存库外,其他每个Bean都显示出来。
作为组件扫描和自动装配的替代方法,这些书描述了将存储库简单地声明为Bean的可能性,这是我所做的。
@Bean
public CustomRepository(NamedParameterJdbcOperations jdbcOperations) {
return new CustomRepositoryImpl(jdbcOperations);
}
之后,Spring能够连线存储库。我看了本书的github code,希望有更好的理解,但不幸的是,只有运行的Bean解决方案在那里实现。
因此,这里是我的问题:
1)什么可能的原因是那里的bean定义,是这样的一个,而不是由组件识别扫描的场景?
2)该项目已经使用Spring JPA Data Repositories,有没有同时使用两种方法的原因?
您可以检查您在@ComponentScan批注中通知哪些软件包,以及软件仓库是否在其子项中。 –
@ComponentScan(basePackages = {“de.myproject。*”})并且存储库位于子包de.myproject.performance中,找到了来自不同子包的所有其他bean,但可能存在一些特殊规则对于@Repositorys我不知道 – aldr