我正在使用Spring Boot
和MongoDB
。
Spring version : 4.3.9
Spring boot version : 1.5.4
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface HotelRepository extends MongoRepository<Hotel,String> {
}
但是,每当我向HotelRepository添加依赖项时,编译器会在com.demo.hotelController中给出错误字段HotelRepository时,就需要一个类型为“com.demo.HotelRepository”的bean,但无法找到该bean。
@RestController
@RequestMapping("/hotel")
public class HotelController {
@Autowired
private HotelRepository hotelRepository;
@GetMapping("/all")
public List<Hotel> getAllHotels(){
return this.hotelRepository.findAll();
}
}
为了解决这个错误,我从我的角度检查了所有方面,但都是徒劳的。我研发的东西。
hotelrepository
将提供一个现成的默认实现。根据Spring文档@repository
注释了接口,因此不需要将@component
@autowired
注释从spring上下文添加依赖项,因此它应该从spring上下文中选择已创建的bean。@componentscan
下面是我的主要spring boot应用程序类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MongoWithBootApplication {
public static void main(String[] args) {
SpringApplication.run(MongoWithBootApplication.class, args);
}
}
应用程序.属性:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=HotelDB
堆栈跟踪:
2017-07-10 13:25:12.485 INFO 4712 --- [ main] com.demo.MongoWithBootApplication : Starting MongoWithBootApplication on KELLGGNCPU0313 with PID 4712 (D:\STS_WS\MongoWithBoot\target\classes started by mehrajuddin.malik in D:\STS_WS\MongoWithBoot)
2017-07-10 13:25:12.487 INFO 4712 --- [ main] com.demo.MongoWithBootApplication : No active profile set, falling back to default profiles: default
2017-07-10 13:25:12.519 INFO 4712 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@13acb0d1: startup date [Mon Jul 10 13:25:12 IST 2017]; root of context hierarchy
2017-07-10 13:25:13.448 INFO 4712 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-10 13:25:13.456 INFO 4712 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-07-10 13:25:13.456 INFO 4712 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-07-10 13:25:13.541 INFO 4712 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-07-10 13:25:13.541 INFO 4712 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1025 ms
2017-07-10 13:25:13.635 INFO 4712 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-07-10 13:25:13.637 INFO 4712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-10 13:25:13.638 INFO 4712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-10 13:25:13.638 INFO 4712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-10 13:25:13.638 INFO 4712 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-10 13:25:13.673 WARN 4712 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hotelController': Unsatisfied dependency expressed through field 'hotelRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.demo.HotelRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-07-10 13:25:13.675 INFO 4712 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2017-07-10 13:25:13.684 INFO 4712 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-07-10 13:25:13.737 ERROR 4712 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field hotelRepository in com.demo.HotelController required a bean of type 'com.demo.HotelRepository' that could not be found.
Action:
Consider defining a bean of type 'com.demo.HotelRepository' in your configuration.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>MongoWithBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MongoWithBoot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
面对类似的问题,在Application类中添加以下内容帮助我解决了这个问题
@EnableMongoRepositories(basePackageClasses = DeviceDataRepository.class)
在你的情况下
@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = HotelRepository.class)
public class MongoWithBootApplication{ ... }
我的问题与@AutoWired类似-找不到依赖项类型的合格bean 我用的是Spring靴。类结构如下: 测试用例尝试和字段,但没有成功。我不确定我的配置有什么问题。由于服务的实现在测试用例的子包中,因此不是必需的。服务类正确使用注释。 完整项目请访问:https://github.com/geoheil/spring 全堆栈跟踪
我有一个安全配置类,其中有一个SecurityWebFilterChain bean。这个bean需要一个ServerHttpSecuitirty实例,但是spring说它找不到任何这种类型的beans,尽管在外部库(org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfiguration
我正在尝试使用注释获得简单的Spring应用程序,但我在运行应用程序时遇到异常... 严重:组织上下文初始化失败。springframework。豆。工厂CannotLoadBeanClassException:找不到在ServletContext资源[/web-INF/mvc dispatcher servlet.xml]中定义的名为“viewResolver”的bean的类[org.sprin
我在尝试用标记化启动Spring Boot应用程序时遇到问题。这是我的服务课: 这是我的配置类: 当我尝试运行我的应用程序时,我会出现以下错误: 我不明白为什么我会犯这个错误。
我在Spring网站上跟踪这个SpringBoot演示,学习如何创建一个接受上传的文件。我收到一条错误消息: 我试图通过向FileUploadController添加注释来修复它。 我已尝试将以下依赖项添加到build.gradle 注意:我不想将数据库连接到此演示项目。这个演示确认它不是在这个演示中尝试连接数据库,而是在生产环境中尝试连接数据库。 我尝试在Storage Service接口上方添
你好,我正在学习Spring Boot,我正在做一个简单的项目。我在尝试执行测试时遇到了这个问题。请告诉我我做错了什么:( 我的项目Github在这里:https://github.com/emicovi/GildedRose_SpringBoot 应用程序无法启动 描述: __________: 字段中的字段项存储库在 com.镀金玫瑰.DBLoader 需要一个类型为“com.镀金玫瑰.镀金玫