![](/img/trans.png)
[英]Spring Boot 2.1 EntityManagerFactory Bean could not be found
[英]could not found bean for MongoRepository (Spring Boot)
我使用的是spring boot
和MongoDB
。
Spring version : 4.3.9
Spring boot version : 1.5.4
我正在創建一個實現MongoRepository
interface
的存儲庫,如下所示
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface HotelRepository extends MongoRepository<Hotel,String> {
}
但是,每當我向HotelRepository編譯器添加依賴項時,在com.demo.HotelController中提供錯誤Field hotelRepository in com.demo.HotelController required a bean of type 'com.demo.HotelRepository' that could not be found.
@RestController
@RequestMapping("/hotel")
public class HotelController {
@Autowired
private HotelRepository hotelRepository;
@GetMapping("/all")
public List<Hotel> getAllHotels(){
return this.hotelRepository.findAll();
}
}
我已經檢查了我身邊的所有方面來解決錯誤,但都是徒勞的。 我研發的是什么。
HotelRepository
,將提供開箱即用的默認實現。 按照春季文檔 @Repository
注釋了接口,所以不需要放@Component
@Autowired
注釋從spring上下文添加依賴項,因此它應該從spring上下文中選擇創建的bean。 @ComponentScan
這是我的主要春季啟動應用程序類:
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);
}
}
application.properties:
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.
pom.xml :
<?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{ ... }
我遇到了同樣的問題
在代碼下方用於掃描mongorepository包
@SpringBootApplication
@EnableMongoRepositories(basePackages = {"//packages you want to scan for activiting mongo repositories"})
public class SpringBootMongoDBApp{ ... }
它解決了我的問題
經過這么多的斗爭,我終於解決了這個問題。
代碼或注釋沒有任何問題。 問題是spring boot的版本 。
但是,我仍然不確定1.5.1.RELEASE
以上版本中的錯誤。如果有人知道根本原因可以編輯或回答問題。
問題 :如果我添加spring-boot-starter-parent
上述1.5.1.RELEASE
,它給了我上面沒有豆錯誤MongoRepository
。 它給出了從1.5.2
到1.5.4
版本的錯誤。 ( 1.5.4
是最后一個版本,直到我嘗試過並面臨無bean錯誤)
解決方案 :如果我添加spring-boot-starter-parent
1.5.1.RELEASE
或更低版本( 1.5.0
是最低版本,直到我嘗試過),它運行正常。
我有相同的結構,我只需添加此配置:
Application.java
@Configuration
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
...
}
application.properties
spring.data.mongodb.uri=mongodb://localhost:27017/hotelDB
並且存儲庫包必須位於Application.java類的更深層次。 就這樣
com.hotelDB
Application.java
repository (Package)
controller (Package)
service (Package)
...
我們需要使用EnableMongoRepositories
激活mongo存儲庫
@SpringBootApplication
@EnableMongoRepositories //specify packages to scan
public class MongoWithBootApplication{ ... }
剛試過,並沒有發現任何問題與我現有的spring boot mongodb項目1.5.4.RELEASE
for spring-boot-starter-parent
。
之前它配置了1.4.0.RELEASE
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.