简体   繁体   English

需要一个无法通过Spring Boot找到的类型的bean

[英]Required a bean of type that could not be found Spring Boot

Spring Boot application fails to start? Spring Boot应用程序无法启动?

Here is my Project Structure 这是我的项目结构

Below is my main class. 下面是我的主班。

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);
        }
    }

And my console is 我的控制台是

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 Application.properties

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

UserRepository interface 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);
}

Here is my controller 这是我的控制器

@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);
    }
}

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

Instead of MongoRepository, I tried CrudRepository interface. 我尝试使用CrudRepository接口代替MongoRepository。 Its working fine. 它的工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring boot 字段需要一个找不到类型的 bean - Spring boot Field required a bean of type that could not be found Spring 引导自动装配不工作。 必需的 bean,类型找不到 - Spring Boot autowire not working. Required bean, of type that could not found 运行 spring 时启动应用程序需要一个找不到类型的 bean - when running spring Boot application getting required a bean of type that could not be found Spring 引导基本应用程序:字段 NotesRepository 需要一个无法找到的“com.demo.NotesRepository”类型的 bean - Spring boot basic application: field NotesRepository required a bean of type 'com.demo.NotesRepository' that could not be found 使用 JOOQ 的 Spring boot 收到一条消息“需要找不到类型为‘org.jooq.DSLContext’的 bean” - Spring boot with JOOQ got a message "required a bean of type 'org.jooq.DSLContext' that could not be found" 如何修复“字段......需要一个......无法找到类型的bean”异常Spring Boot - How to fix "Field ... required a bean of type ... that could not be found" exception Spring Boot 如何修复“字段......需要一个类型的bean......无法找到”异常Spring Boot - How to fix “Field … required a bean of type … that could not be found” exception Spring Boot Spring Boot:需要一个名为&#39;entityManagerFactory&#39;的bean无法找到 - Spring Boot : required a bean named 'entityManagerFactory' that could not be found Spring-boot:需要一个无法找到的名为“entityManagerFactory”的 bean - Spring-boot: required a bean named 'entityManagerFactory' that could not be found Spring 引导 - 存储库字段需要一个名为“entityManagerFactory”的 bean,但找不到 - Spring Boot - repository field required a bean named 'entityManagerFactory' that could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM