2017-05-05 72 views
0

我是Spring引导和gradle的新手。我正在创建一个Spring引导应用程序,并且想要使用JDBC模板连接。我build.gradle是:java.lang.ClassNotFoundException:更改build.gradle时的javax.persistence.EntityManagerFactory

buildscript { 
    ext { 
     springBootVersion = '1.5.2.RELEASE' 
    } 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    } 
} 


apply plugin: 'java' 
apply plugin: 'eclipse-wtp' 
apply plugin: 'org.springframework.boot' 

sourceCompatibility = 1.8 
targetCompatibility = 1.8 

repositories { 
    jcenter() 
} 

dependencies { 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.2.RELEASE' 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-security', version: '1.5.2.RELEASE' 

    compile project(':supplier-commons') 
    runtime project(':supplier-commons') 

    compile 'org.springframework:spring-context:4.2.6.RELEASE' 
    compile 'org.springframework:spring-webmvc:4.2.6.RELEASE' 
    compile 'org.springframework.data:spring-data-jpa:1.10.1.RELEASE' 
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6' 
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.7' 
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.7' 
    compile group: 'com.typesafe.akka', name: 'akka-actor_2.11', version: '2.4.1' 
    compile 'io.jsonwebtoken:jjwt:0.7.0' 
    compile group: 'javax.mail', name: 'mail', version: '1.4' 
    compile group: 'freemarker', name: 'freemarker', version: '2.3.9' 
    compile group: 'org.springframework.security', name: 'spring-security-crypto', version: '4.2.2.RELEASE' 
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.4' 
    compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.9' 
    compile group: 'org.jmockit', name: 'jmockit', version: '1.8' 
    compile 'io.jsonwebtoken:jjwt:0.7.0' 
    compile group: 'com.ning', name: 'async-http-client', version: '1.9.40' 
    compile group: 'org.mockito', name: 'mockito-all', version: '1.8.4' 
    compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.0.7' 

    compile('org.springframework.boot:spring-boot-starter-jdbc'){ 
     exclude module : 'spring-boot-starter-logging' 
     exclude module: "logback-classic" 
    } 
    runtime('org.springframework.boot:spring-boot-devtools') 
    compileOnly('org.springframework.boot:spring-boot-configuration-processor') 

    testCompile 'junit:junit:4.12' 

} 

springBoot { 
    executable = true 
} 

eclipse { 

    wtp { 
    facet { 
     facet name: 'jst.web', version: '2.4' 
    } 

    component { 
     contextPath = 'aggregator-api' 
    } 

    } 
} 

当我运行应用程序,我得到以下错误:

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[email protected]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[email protected]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) 

但是当我删除

compile('org.springframework.boot:spring-boot-starter-jdbc'){ 
     exclude module : 'spring-boot-starter-logging' 
     exclude module: "logback-classic" 
    } 

我没有得到任何错误,但比我不能使用jdbc连接。

任何解决方案?

+0

尝试[this](http://stackoverflow.com/questions/25639515/java-lang-classnotfoundexception-javax-persistence-persistence-cannot-be-found) – XtremeBaumer

回答

1

对于初学者清理你的构建,你试图在那里太聪明。 Spring Boot有很多初学者,你正在尝试绕过。

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管理版本) 。

相关问题