【问题标题】:SpringBootApplication is unable to find bean under subpackageSpringBootApplication在子包下找不到bean
【发布时间】:2021-10-03 11:57:12
【问题描述】:

我在启动 SpringBootApplication 时遇到了这个错误。

Description:

Field XXX in com.helloworld.www.batch.SomeServiceImpl required a bean of type 'com.helloworld.www.batch.repository.SomeRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.helloworld.www.batch.repository.SomeRepository' in your configuration.

这是我的项目布局,其中有我的 SpringBootApplication 和存储库。我用“@SpringBootApplication”注释了我的 SpringBootApplication,但它找不到“SomeRepository”bean 类。

我是否必须用“EnableJpaRepositories”注释它并指出存储库包,然后它才能检测到“SomeRepository”bean 类?

com.helloworld.www
+SpringBootApplicationName.java
com.helloworld.www.batch.repository
+SomeRepositoryName.java

更新帖子,我已经用@Repository注释了我的Repository,它只包含一个没有任何实现类的接口。

@Repository
public interface SomeRepository extends PagingAndSortingRepository<SomeModel, Integer> { }

这是我在“com.helloworld.www”包下的主要应用程序类

@SpringBootApplication
public class SomeApplication {
    public static void main(String[] args) {
        SpringApplication.run(SomeApplication.class);
    }
}

【问题讨论】:

  • 你有没有用@Repository注解对repository接口进行注解?
  • @vincendep 这完全没有必要。
  • 所以在扫描组件时,即使没有注释,存储库也会被注册为 bean?
  • 我确实用 @Repository 注释了我的课程。
  • 您好,请在您的问题中包含带有@SpringBootApplication 注解的类

标签: java spring spring-boot


【解决方案1】:

我认为这不是一个“简单”的注入问题。 看起来您正在使用弹簧数据。 而且必须注入的Repository其实是一个接口,而不是具体的bean实现。

Spring Data 引擎应该能够“找到”您的存储库并在运行时创建一个真正的实现(由代理机制支持)。然后它将这个生成的代理放到应用程序上下文中,然后才能将其注入到其他 bean 中。

在这种情况下,看起来这个将存储库转换为 bean 的“引擎”由于某种原因没有工作,因此在服务实例化时 (SomeServiceImpl) 没有实际的 bean 代表 (已从)存储库中生成。

很难说更多,因为你没有展示任何与spring数据相关的配置,也没有提供那个Repository的代码......

【讨论】:

  • 我已经用@Repository注解了我的repository接口,但是没有实现类。
猜你喜欢
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 2018-12-26
  • 2016-08-10
相关资源
最近更新 更多