The*_*ger 5 spring repository javabeans autowired spring-boot
我的项目中有这样的层次结构:
\n\xe2\x96\xbc server\n \xe2\x96\xbc myproject\n \xe2\x96\xbc src\n \xe2\x96\xbc main\n \xe2\x96\xbc java\n \xe2\x96\xbc rest\n \xe2\x96\xbc repository\n \xe2\x92\xbe MyRepository\n \xe2\x96\xb6 resources\n \xe2\x96\xbc test\n \xe2\x96\xbc java\n \xe2\x96\xbc rest\n \xe2\x92\xb8 MyRepositoryTest\nRun Code Online (Sandbox Code Playgroud)\n这是MyRepository界面:
\npublic interface MyRepository extends MongoRepository<String, Integer> {\n}\nRun Code Online (Sandbox Code Playgroud)\n这是MyRepositoryTest测试类:
\n@ExtendWith(SpringExtension.class)\npublic class MyRepositoryTest {\n ...\n @Autowired MyRepository myRepository;\n ...\n}\nRun Code Online (Sandbox Code Playgroud)\n该错误发生在测试类中自动装配的 myRepository 实例上。它说Could not autowire. No beans of 'MyRepository' type found。我进行了一些搜索并尝试添加等等,但没有任何帮助@Component。@Repository我该如何解决这个问题?
你可以尝试这个,将你的包设置在@ComponentScan类中AppConfig:
@ContextConfiguration(classes = AppConfig.class)
@ExtendWith(SpringExtension.class)
public class AdminEvaluatorTest {
@Autowired MyRepository myRepository;
@Configuration
@ComponentScan("com.<your-package>")
public static class AppConfig {
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20190 次 |
| 最近记录: |