Java 所需的bean类型在Spring Boot中找不到
Spring启动应用程序无法启动 下面是我的主课Java 所需的bean类型在Spring Boot中找不到,java,mongodb,spring-boot,Java,Mongodb,Spring Boot,Spring启动应用程序无法启动 下面是我的主课 package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Start { public static void mai
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.
应用程序属性
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=example
用户存储库接口
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);
}
尝试在
开始
类中添加@EnableMongoRepositories(basePackages=“com.example.repository”)
。我尝试了CrudePository接口,而不是MongoRespository。工作正常。可能有很多原因。请提供更多配置细节,因为可能有许多原因导致UserRepository
无法映射。我在控制台部分添加了更多细节。plz check您可以检查实现此接口的类com.example.repository.UserRepository是否在类路径上可用,以及是否用@repository annotation注释更新您的问题吗application.properties将控制器代码和存储库发布在此处。如我所述,它将在启动类中放置在repository?上。在您的案例中,Start.class我又遇到了一个错误,如org.springframework.beans.factory.unsatifiedpendencyException:创建名为“userController”的bean时出错:对于使用生成io.swagger.Swagger2SpringBoot的swagger codegen工具的用户,通过字段“userRepository”表示未满足的依赖关系。是,将位添加到“@ComponentScan”中无效。由于某些原因,Spring2.1.5无法在io.swagger之外查看,所以这个补丁对我来说很有效@“存储库”也不起作用。我说的是MongoDb的设置,伙计们。
@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);
}
}