java.lang.ClassNotFoundException:javax.persistence.EntityManagerFactory更改build.gradle时

java.lang.ClassNotFoundException:javax.persistence.EntityManagerFactory更改build.gradle时,java,spring,gradle,spring-boot,build.gradle,Java,Spring,Gradle,Spring Boot,Build.gradle,我对spring boot和gradle是新手。我正在创建一个spring启动应用程序,希望使用JDBC模板连接。我的build.gradle是: 运行应用程序时,出现以下错误: java.lang.NoClassDefFoundError: javax/persistence/EntityManagerFactory at org.springframework.data.jpa.util.BeanDefinitionUtils.<clinit>(BeanDefinitio

我对spring boot和gradle是新手。我正在创建一个spring启动应用程序,希望使用JDBC模板连接。我的build.gradle是:

运行应用程序时,出现以下错误:

java.lang.NoClassDefFoundError: javax/persistence/EntityManagerFactory
    at org.springframework.data.jpa.util.BeanDefinitionUtils.<clinit>(BeanDefinitionUtils.java:53) ~[spring-data-jpa-1.10.1.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.EntityManagerBeanDefinitionRegistrarPostProcessor.postProcessBeanFactory(EntityManagerBeanDefinitionRegistrarPostProcessor.java:54) ~[spring-data-jpa-1.10.1.RELEASE.jar:na]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:181) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at com.etl.aapi.springboot.main.AggregatorApplication.main(AggregatorApplication.java:14) [bin/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_112]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_112]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_112]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.2.RELEASE.jar:1.5.2.RELEASE]
Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityManagerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_112]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_112]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_112]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_112]
    ... 18 common frames omitted

2017-05-05 11:46:05.465  INFO 10960 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@67a280dd: startup date [Fri May 05 11:46:03 IST 2017]; root of context hierarchy
2017-05-05 11:46:05.468  WARN 10960 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Exception thrown from LifecycleProcessor on context close

java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@67a280dd: startup date [Fri May 05 11:46:03 IST 2017]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:415) [spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:975) [spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:934) [spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:794) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:325) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at com.etl.aapi.springboot.main.AggregatorApplication.main(AggregatorApplication.java:14) [bin/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_112]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_112]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_112]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
我没有得到任何错误,但是我不能使用jdbc连接


有什么解决方案吗?

对于刚开始清理构建的人来说,你在这方面太聪明了。SpringBoot有很多启动器,您正在包括它们,但也试图绕过它们

dependencies {
    compile project(':supplier-commons')

    compile ('org.springframework.boot:spring-boot-starter') {
      exclude module : 'spring-boot-starter-logging'
    }
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-freemarker'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2'

    compile group: 'mysql', name: 'mysql-connector-java'
    compile group: 'com.typesafe.akka', name: 'akka-actor_2.11', version: '2.4.1'
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.4'
    compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.9'
    compile 'io.jsonwebtoken:jjwt:0.7.0'
    compile group: 'com.ning', name: 'async-http-client', version: '1.9.40'
    compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.0.7'

    testCompile group: 'org.jmockit', name: 'jmockit', version: '1.8'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'

    runtime('org.springframework.boot:spring-boot-devtools')
    compileOnly('org.springframework.boot:spring-boot-configuration-processor')
}
差不多吧。这将引入Spring Boot定义的所有版本注意,许多依赖项没有指定版本,对于您声明的技术,您希望使用启动器,以便获得Spring Boot管理的版本

试一试
compile('org.springframework.boot:spring-boot-starter-jdbc'){
        exclude module : 'spring-boot-starter-logging'
        exclude module: "logback-classic"
    }
dependencies {
    compile project(':supplier-commons')

    compile ('org.springframework.boot:spring-boot-starter') {
      exclude module : 'spring-boot-starter-logging'
    }
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-freemarker'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-mail'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2'

    compile group: 'mysql', name: 'mysql-connector-java'
    compile group: 'com.typesafe.akka', name: 'akka-actor_2.11', version: '2.4.1'
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.4'
    compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.9'
    compile 'io.jsonwebtoken:jjwt:0.7.0'
    compile group: 'com.ning', name: 'async-http-client', version: '1.9.40'
    compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.0.7'

    testCompile group: 'org.jmockit', name: 'jmockit', version: '1.8'
    testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test'

    runtime('org.springframework.boot:spring-boot-devtools')
    compileOnly('org.springframework.boot:spring-boot-configuration-processor')
}