[英]Parameter 0 of constructor in [] required a bean of type [] that could not be found
Error : Parameter 0 of constructor in com.tw.api.service.impl.BookServiceImpl required a bean of type 'com.tw.api.repository.BookRepository' that could not be found.错误:com.tw.api.service.impl.BookServiceImpl 中构造函数的参数 0 需要一个无法找到类型为“com.tw.api.repository.BookRepository”的 bean。
Exception : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookController' defined in file [/Users/rashdul.ehasan/Projects/boot-camp/api/target/classes/com/tw/api/controller/BookController.class]: Unsatisfied dependency expressed through constructor parameter 0;异常:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“bookController”的 bean 在文件 [/Users/rashdul.ehasan/Projects/boot-camp/api/target 中定义时出错/classes/com/tw/api/controller/BookController.class]:通过构造函数参数0表达的未满足的依赖; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImpl' defined in file [/Users/rashdul.ehasan/Projects/boot-camp/api/target/classes/com/tw/api/service/impl/BookServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“bookServiceImpl”的 bean 在文件 [/Users/rashdul.ehasan/Projects/boot-camp/api/target/classes/com/tw/api/ 中定义时出错service/impl/BookServiceImpl.class]:通过构造函数参数0表示的未满足的依赖; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.tw.api.repository.BookRepository' available: expected at least 1 bean which qualifies as autowire candidate.嵌套的异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用类型 'com.tw.api.repository.BookRepository' 的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选者。 Dependency annotations依赖注解
Any idea on how to fix this?关于如何解决这个问题的任何想法?
I have excluded the default auto configuration...我已经排除了默认的自动配置......
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class})
public class ApiApplication {
I have written a factory bean class for mongo but it does not seem to reach there.我为 mongo 写了一个工厂 bean class 但它似乎没有到达那里。
@Configuration
@ConditionalOnProperty(prefix = "spring", name = "db.dialect", havingValue = "mongo", matchIfMissing = true)
@EnableMongoRepositories(
repositoryFactoryBeanClass = ApiMongoRepositoryFactoryBean.class,
basePackages = "com.tw.api.repository")
public class ApiMongoRepositoryConfig {
@Value("${spring.data.mongodb.uri}")
private String uri;
private static final Logger LOGGER = LoggerFactory.getLogger(ApiMongoRepositoryConfig.class);
public ApiMongoRepositoryConfig() {
LOGGER.info("Repository Configuration: " + ApiMongoRepositoryConfig.class);
}
@Bean
public MongoClient mongoClient() {
return new MongoClient(new MongoClientURI(uri));
}
}
Packages:套餐:
BookController: package com.tw.api.controller;
BookService: package com.tw.api.service;
BookServiceImpl: package com.tw.api.service.impl;
BookRepository: package com.tw.api.repository;
ApiMongoRepository: package com.tw.api.repository.base;
ApiMongoRepositoryConfig: package com.tw.api.config;
ApiMongoRepositoryFactoryBean: package com.tw.api.helper;
ApiApplication: package com.tw.api;
Here is the link to the code base, https://github.com/er310/boot-camp/tree/master/api这是代码库的链接, https://github.com/er310/boot-camp/tree/master/api
You have your Book
class marked as a JPA entity:您将Book
class 标记为 JPA 实体:
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "book")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Book extends AbstractEntity {
But you are only creating Mong repositories with @EnableMongoRepositories
.但是您只是使用@EnableMongoRepositories
创建 Mong 存储库。
You need to make up your mind if you want to use MongoDb with Spring Data Mongo or a relational database with JPA + Spring Data JPA. MongoDb 和 Spring Data Mongo 还是关系型数据库 JPA + Spring Data JPA 需要拿定主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.