java - 无法将名为“entityManagerFactory”的 bean 识别为使用存储库的自动装配注释
问题描述
错误
应用程序无法启动
描述:
Field ProRepo in com.pvs.products.testservice.ProService required a bean named 'entityManagerFactory' that could not be found.
The injection point has the following annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)
行动:
考虑在配置中定义一个名为“entityManagerFactory”的 bean。
代码
构建.gradle
plugins {
id 'org.springframework.boot' version '2.1.12.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
id 'war'
id 'java-library'
id "com.dorongold.task-tree" version "1.4"
}
group = 'com.pvs'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
jcenter()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'io.springfox:springfox-swagger2:2.6.1'
compile 'io.springfox:springfox-swagger-ui:2.6.1'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation "com.fasterxml.jackson.module:jackson-module-kotlin"
implementation "org.jetbrains.kotlin:kotlin-reflect"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation "com.okta.spring:okta-spring-boot-starter:1.2.1"
implementation 'org.apache.cxf:cxf-rt-frontend-jaxrs:3.1.6'
//Spring Framework
// implementation 'org.springframework.security:spring-security-config:4.2.8.RELEASE'
// implementation 'org.springframework.security:spring-security-core:4.2.8.RELEASE'
// implementation 'org.springframework.security:spring-security-web:4.2.8.RELEASE'
implementation 'org.springframework:spring-core:5.1.5.RELEASE'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
// testImplementation 'org.springframework.security:spring-security-test'
compile group: 'org.springframework', name: 'spring-core', version: '5.1.5.RELEASE'
implementation 'org.springframework:spring-jdbc:4.3.20.RELEASE'
implementation 'org.springframework:spring-orm:4.3.20.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-data-solr'
// implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
// implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
// compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure', version: '2.1.2.RELEASE'
compile 'org.mariadb.jdbc:mariadb-java-client:1.5.7'
compile 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.2.1.RELEASE'
//JDBC
// implementation 'org.springframework.boot:spring-boot-starter-jdbc'
// implementation 'org.springframework.session:spring-session-jdbc'
implementation 'org.springframework:spring-jdbc:4.3.20.RELEASE'
//Hibernate
implementation 'org.hibernate:hibernate-core:5.2.12.Final'
implementation 'org.hibernate:hibernate-ehcache:5.2.12.Final'
testCompile 'org.hibernate:hibernate-ehcache:5.2.12.Final'
compile 'org.apache.commons:commons-lang3:3.1'
implementation 'org.apache.lucene:lucene-core:6.3.0'
implementation 'org.apache.solr:solr-core:6.3.0'
implementation 'org.apache.solr:solr-solrj:6.3.0'
implementation 'org.apache.solr:solr-velocity:6.3.0'
implementation 'com.zaxxer:HikariCP:3.2.0'
compile (group: 'org.apache.solr', name: 'solr-velocity', version: '6.3.0') {
exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
}
compile group: 'org.bytedeco', name: 'javacv-platform', version: '1.5.2'
//XML and JSON
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-hibernate5'
//Java org
implementation 'net.sf.jpf:jpf-boot:1.5'
implementation 'net.sf.jpf:jpf:1.5'
implementation 'com.ibm.icu:icu4j:3.8'
//MySQL
implementation 'org.hsqldb:hsqldb:2.2.8'
implementation 'org.hsqldb:sqltool:2.2.8'
//Hibernate
//compile 'org.hibernate:hibernate-c3p0:4.1.9.Final'
compile 'org.hibernate:hibernate-ehcache:4.1.9.Final'
//Lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// newly added
compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.4.12.Final'
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.1'
compile group: 'org.springframework.data', name: 'spring-data-redis', version: '2.1.12.RELEASE'
// //commented above librabry with old version
// compile group: 'org.springframework.boot', name: 'spring-boot-autoconfigure'
// compile group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '2.2.4.RELEASE', ext: 'pom'
}
应用程序.yml
spring:
main:
allow-bean-definition-overriding: true
security:
username: abc
password: abc
datasource:
driverClassName: org.mariadb.jdbc.Driver
url: jdbc:mariadb:failover://192.168.100.101:3306/tentDB?autoReconnect=true
username: root
password: roor
timeBetweenEvictionRunsMillis: 1000
testWhileIdle: true
validationQuery: SELECT 1
jpa:
hibernate.ddl-auto: update
generate-ddl: true
show-sql: true
logging:
level:
org:
springframework:
security: DEBUG
endpoints:
info:
enabled: true
id: information
sensitive: false
management:
security:
enabled: true
endpoints:
shutdown:
enabled: true
enabled-by-default: false
info:
enabled: true
web:
base-path: /
exposure:
include: *
模型
@Entity
@Table(name = "pv_product")
public class product{
//properties are defined inside
}
存储库
@Repository
public interface ProRepo extends JpaRepository<pv_product, BigInteger> {
@Override
List<pv_product> findAll();
}
服务
@Component
public class ProService {
@Autowired
ProRepo proRepo;
//methods are implemented
}
- 我正在使用 Gradle 导入
- 已导入 JPA 依赖项
@SpringBootApplication(scanBasePackages={"com.pvs.product", "com.pvs.pvscore"})
@EnableJpaRepositories
更新 删除 DataSourceAutoCondifguration 后出现此错误;
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is java.lang.NoSuchFieldError: HANA
解决方案
请从 hibernate 依赖项、spring-jdbc 和 spring-orm 中清除您的 gradle.build 文件。由于 spring-boot-starter-jpa 具有 hibernate 作为传递依赖。
推荐阅读
- authentication - npm login --always-auth doesn't add the appropriate entry to the config
- python - How do I read a CSV file using Pandas then split the columns into two Numpy arrays using to_numpy for Scikit-Learn?
- azerothcore - 在游戏连接中禁用并允许创建角色 azerothcore
- android - how to delete (photo ,video , file) in android 9
- acumatica - 如何在网格视图委托中覆盖账单和调整屏幕 AddPOReceipt 按钮
- javascript - 为什么在 Dynamics 365 中对 html Web 资源使用 http 请求创建注释后会收到 500 内部服务器错误?
- c++ - 如何在模板参数中允许类类型,只有当它有两个基类时
- python - 在 Pandas 中创建具有多个条件的箱线图
- python - 在 Visual Studio 中使用 Pythonnet (Python for .Net) 的问题
- java - Jenkins artifactory maven 步骤使用 maven 程序集插件(Artifactory plugin 3.5.0)静默失败