我正在嘗試使用 MongoRepository,但 Spring 抱怨說,背景關系中沒有 MonoRepository-Interface 的實體。
@SpringBootApplication
public class BackendApplication {
public static void main(String[] args) {
SpringApplication.run(BackendApplication.class, args);
}
}
@Document(collection = "tableItems")
class TableItem {
@Id
public String id;
public int roundNbr;
}
interface TableItemsRepository extends MongoRepository<TableItem, String> {
}
@Service
class TableItemsService {
@Autowired
public TableItemsService(TableItemsRepository tableItemsRepository) {
}
}
服務器抱怨:
Parameter 0 of constructor in backend.TableItemsService required a bean of type 'backend.TableItemsRepository' that could not be found.
Action:
Consider defining a bean of type 'backend.TableItemsRepository' in your configuration.
我的Maven pom.xml:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.4.0</version>
</dependency>
如何獲取 MongoRepository 的 bean 實體?
uj5u.com熱心網友回復:
依賴不正確。
spring-data-mongodb只讓它編譯:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.4.0</version>
</dependency>
但是您需要額外的依賴項才能使其運行。為方便起見,您可以 import spring-boot-starter-data-mongodb,它將所有必需的工件作為傳遞依賴項匯入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/479505.html
