【问题标题】:Required a bean of type that could not be found Spring Boot需要一个找不到 Spring Boot 类型的 bean
【发布时间】:2018-05-11 12:39:42
【问题描述】:

Spring Boot 应用启动失败?

Here is my Project Structure

下面是我的主要课程。

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

我的控制台是

2017-11-28 11:48:52.187  WARN 7316 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.repository.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Description:

Field userRepository in com.example.controller.UserController required a bean of type 'com.example.repository.UserRepository' that could not be found.


Action:

Consider defining a bean of type 'com.example.repository.UserRepository' in your configuration.

Application.properties

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=example

UserRepository 接口

package com.example.repository;

import org.springframework.data.mongodb.repository.MongoRepository;

import com.example.model.User;

public interface UserRepository extends MongoRepository<User, String>{
    public User findOneByName(String name);
}

这是我的控制器

@RestController
@RequestMapping("/user/")
public class UserController {
    @Autowired
    public UserRepository userRepository;
    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public void create(@RequestBody User user){
        userRepository.save(user);
    }
}

【问题讨论】:

  • 可能有很多原因。请提供更多配置细节,因为UserRepository 未被映射的原因可能有很多。
  • 我在控制台部分添加了更多细节。请检查
  • 您能否检查实现此接口 com.example.repository.UserRepository 的类是否在类路径中可用以及是否使用 @Repository 注释进行注释
  • 使用 application.properties 更新您的问题
  • 在此处发布您的控制器代码和存储库。

标签: java mongodb spring-boot


【解决方案1】:

我尝试了 CrudRepository 接口,而不是 MongoRepository。它工作正常。

【讨论】:

    【解决方案2】:

    尝试在Start 类中添加@EnableMongoRepositories(basePackages="com.example.repository")

    【讨论】:

    • 它会放在存储库中吗?
    • 在启动课上,正如我所提到的。 Start.class 在你的情况下
    • 我又遇到了一个错误,例如 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“userController”的 bean 时出错:通过字段“userRepository”表示不满足的依赖关系
    • 对于那些使用生成 io.swagger.Swagger2SpringBoot 的 swagger codegen 工具的人。是的,将您的位添加到“@ComponentScan”中不起作用。由于某种原因,Spring 2.1.5 不能在 io.swagger 之外查看,所以这个修复对我有用。 '@Repository' 也不起作用。我说的是这里的 MongoDb 设置。
    猜你喜欢
    • 2019-02-09
    • 2020-05-09
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 2018-08-13
    相关资源
    最近更新 更多