使用JavaConfig我在定位@Repository
Spring bean时遇到问题。
存储库接口的定义如下:
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
配置的定义如下:
@Configuration
@ComponentScan("com.example")
@EnableAutoConfiguration
@EnableJpaRepositories("com.example")
public class SampleApplication extends SpringBootServletInitializer {
...
包结构如下所示:
com.example
configuration
SampleApplication
repository
UserRepository
在日志文件中,我看到可以找到 Repository 作为bean定义的候选者,但是:
ClassPathBeanDefinitionScanner | Ignored because not a concrete top-level class:
趣味事实
如果我将SampleApplication类移动到com.example
包中,一切将开始工作。
有什么想法我想念的吗?
@EntityScan
如果您的JPA实体不在的子包中,则可能需要使用Spring Boot的注释com.example.configuration
。我也建议你移动你的@Configuration
离开SpringBootServletInitializer
,并进入自己的类。
如果您可以将配置类上移,则可以将@ComponentScan
,@EnableJpaRepositories
和@EntityScan
注释放在一起(请参阅http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-locating-主要班级)
如果@EntityScan
不能解决问题,也许您可以提供一个示例项目供我们参考?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句