【问题标题】:I keep getting this @bean error when trying to run my spring boot app with dynamodb and graphql尝试使用 dynamodb 和 graphql 运行我的 Spring Boot 应用程序时,我不断收到此 @bean 错误
【发布时间】:2020-04-06 01:12:35
【问题描述】:

这是我得到的错误:

Description:
Field andiRepository in com.service.datafetcher.AllAndisDataFetcher required a bean of type 'com.repositories.AndiRepository' 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.repositories.AndiRepository' in your configuration.

这是需要 bean 的数据获取器文件:

import com.models.Andi;
import com.repositories.AndiRepository;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class AllAndisDataFetcher implements DataFetcher<List<Andi>> {
    @Autowired
    AndiRepository andiRepository;
    @Override
    public List<Andi> get(DataFetchingEnvironment dataFetchingEnvironment) throws Exception {
        return andiRepository.findAll();
    }
}

这是驻留在“com”中的主要方法。

package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@ComponentScan("com.repositories")//to scan repository files
@EntityScan("com.models")
@EnableJpaRepositories("com.repositories.AndiRepository")
public class DynamoDBApplication {
   public static void main(String[] args) {
      SpringApplication.run(DynamoDBApplication.class, args);
   }
}

模型、存储库、服务、包都在主 com 包中。

这是存储库文件:

package com.repositories;

import com.andiskillsmaxmodels.Andi;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AndiRepository extends CrudRepository<Andi, Integer> {
}

谢谢

【问题讨论】:

  • 能否指定main()方法所在类的完整包名?
  • 我会把它添加到问题中,谢谢
  • 我已经添加了。
  • 这是一个 maven 多模块项目吗?
  • 我不确定这意味着什么,但这应该是一个简单的端点,它在正文中接收一个 graphql 查询,然后在 dynamodb 上执行它。

标签: java spring spring-boot graphql amazon-dynamodb


【解决方案1】:

更正您的软件包名称。他们到处都是。您似乎从不同的包中导入了类。确保 AndiRepositorycom.repositories 包中。和Andi 类在你的com.models 包中。纠正这些错误后,请执行以下操作。

删除@ComponentScan("com.repositories")。你不需要这个,因为@SpringBootApplication自动为你做。

并将@SpringBootApplication 替换为@SpringBootApplication(scanBasePackages = "com")

【讨论】:

  • 嗨,谢谢。包名称已更正,我添加了注释。应用程序启动但收到 404 错误。当我删除组件扫描时,我得到了同样的错误。
  • @AmanKhan 既然你现在可以运行你的程序,你的问题已经解决了。这是一个单独的问题。因此,您可以将其标记为正确答案。对于404 错误,它一定是您的端点或您调用端点的方式有问题。找不到您的端点。这就是它的意思。因此,请正确检查您的控制器。我对 GraphQL 没有任何经验。如果它运行类似于 REST,我可能会提供帮助。
猜你喜欢
  • 1970-01-01
  • 2016-04-09
  • 1970-01-01
  • 2019-11-22
  • 2020-10-06
  • 1970-01-01
  • 2011-12-13
  • 2012-05-16
  • 1970-01-01
相关资源
最近更新 更多