问题
Error
APPLICATION FAILED TO START
Description:
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)
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
Code
build.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'
}
application.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: *
Model
@Entity
@Table(name = "pv_product")
public class product{
//properties are defined inside
}
Repository
@Repository
public interface ProRepo extends JpaRepository<pv_product, BigInteger> {
@Override
List<pv_product> findAll();
}
Service
@Component
public class ProService {
@Autowired
ProRepo proRepo;
//methods are implemented
}
- I am using Gradle imports
- JPA dependency already imported
@SpringBootApplication(scanBasePackages={"com.pvs.product", "com.pvs.pvscore"})
@EnableJpaRepositories
Update After removing DataSourceAutoCondifguration it gave this error;
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
回答1:
Please clean your gradle.build file from hibernate dependencies , spring-jdbc and spring-orm. As spring-boot-starter-jpa has hibernate as transitive dependency.
来源:https://stackoverflow.com/questions/60255958/unable-to-field-identify-bean-named-entitymanagerfactory-as-using-auto-wiring