Spring JPA 无法 autowire/inject bean 超出单元测试,我做错了什么?
Spring JPA is failing to autowire/inject bean beyond unit test, what have I done wrong?
我保证我已经花了很多时间阅读 spring.io 上的文档,尝试快速入门教程等,但我就是无法弄清楚我做错了什么。我也在这里查看了类似的问题,并尝试了建议的解决方案(如果相关)。我敢肯定这是非常简单的事情,所以我很抱歉不得不问。
我的存储库:
@Repository
public interface SomethingRepository extends CrudRepository<Something, Integer> {
}
配置:
@Configuration
@EnableAutoConfiguration
@EntityScan("com.where.they.are") //changed for anonymity - assume correct
@EnableJpaRepositories("com.where.they.are") //changed for anonymity - assume correct
public class DatabaseConfigurationInTest {
}
这里是一些 class 我试图注入我的存储库的地方(它失败了),它总是空的:
public class SomethingDAO{
@Inject
private SomethingRepository somethingRepository;
}
但是在这个单元测试中,它工作得很好,注入等等。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DatabaseConfigurationInTest.class)
@ActiveProfiles("local")
public class SomethingRepoTest{
@Inject
private SomethingRepository somethingRepository;
对于@Predrag Maric 点,你的 class SomethingDAO
可以用 @Service 注释并确保它的包可以通过 componentScan
@Service
public class SomethingDAO{
@Inject
private SomethingRepository somethingRepository;
}
我保证我已经花了很多时间阅读 spring.io 上的文档,尝试快速入门教程等,但我就是无法弄清楚我做错了什么。我也在这里查看了类似的问题,并尝试了建议的解决方案(如果相关)。我敢肯定这是非常简单的事情,所以我很抱歉不得不问。
我的存储库:
@Repository
public interface SomethingRepository extends CrudRepository<Something, Integer> {
}
配置:
@Configuration
@EnableAutoConfiguration
@EntityScan("com.where.they.are") //changed for anonymity - assume correct
@EnableJpaRepositories("com.where.they.are") //changed for anonymity - assume correct
public class DatabaseConfigurationInTest {
}
这里是一些 class 我试图注入我的存储库的地方(它失败了),它总是空的:
public class SomethingDAO{
@Inject
private SomethingRepository somethingRepository;
}
但是在这个单元测试中,它工作得很好,注入等等。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DatabaseConfigurationInTest.class)
@ActiveProfiles("local")
public class SomethingRepoTest{
@Inject
private SomethingRepository somethingRepository;
对于@Predrag Maric 点,你的 class SomethingDAO
可以用 @Service 注释并确保它的包可以通过 componentScan
@Service
public class SomethingDAO{
@Inject
private SomethingRepository somethingRepository;
}