我如何修复无法自动布线。没有找到'MyRepository'类型的Bean? [英] How do I fix "Could not autowire. No beans of 'MyRepository' type found"?

查看:27
本文介绍了我如何修复无法自动布线。没有找到'MyRepository'类型的Bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有此层次结构:

▼ server
  ▼ myproject
    ▼ src
      ▼ main
        ▼ java
          ▼ rest
            ▼ repository
              Ⓘ MyRepository
        ▶ resources
      ▼ test
        ▼ java
          ▼ rest
            Ⓒ MyRepositoryTest

这是我的存储库界面:

public interface MyRepository extends MongoRepository<String, Integer> {
}

这是MyRepositoryTest测试类:

@ExtendWith(SpringExtension.class)
public class MyRepositoryTest {
    ...
    @Autowired MyRepository myRepository;
    ...
}

该错误发生在测试类中自动连接的myRepository实例上。上面写着Could not autowire. No beans of 'MyRepository' type found。我搜索了一下,尝试添加@Component@Repository等等,但都没有真正起到作用。如何解决此问题?

推荐答案

您可以尝试此操作,将您的包设置在AppConfig类的@ComponentScan中:

@ContextConfiguration(classes = AppConfig.class)
@ExtendWith(SpringExtension.class)
public class AdminEvaluatorTest {
    @Autowired MyRepository myRepository;

    @Configuration
    @ComponentScan("com.<your-package>")
    public static class AppConfig {
    }
}

这篇关于我如何修复无法自动布线。没有找到&#39;MyRepository&#39;类型的Bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆