2016-02-05 140 views
0

錯誤7405 --- [main] o.s.boot.SpringApplication:應用程序啓動失敗

我正在使用Gradle 2.9和springBootVersion ='1.3.1.RELEASE' 構建成功但jar部署不成功。這是我的日誌:

2016-02-05 20:44:13.905 WARN 7405 --- [   main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl$Work 
2016-02-05 20:44:13.916 INFO 7405 --- [   main] o.apache.catalina.core.StandardService : Stopping service Tomcat 
2016-02-05 20:44:13.942 ERROR 7405 --- [   main] o.s.boot.SpringApplication    : Application startup failed 

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl$Work 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1054) ~[spring-context-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:829) ~[spring-context-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.1.RELEASE.jar!/:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) [spring-boot-1.3.1.RELEASE.jar!/:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) [spring-boot-1.3.1.RELEASE.jar!/:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-1.3.1.RELEASE.jar!/:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) [spring-boot-1.3.1.RELEASE.jar!/:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113) [spring-boot-1.3.1.RELEASE.jar!/:1.3.1.RELEASE] 
    at com.opensecret.OpenSecretApplication.main(OpenSecretApplication.java:10) [open-secret-0.0.1-SNAPSHOT.jar!/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65] 
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65] 
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53) [open-secret-0.0.1-SNAPSHOT.jar!/:na] 
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_65] 
Caused by: java.lang.NoClassDefFoundError: org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl$Work 
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:54) ~[spring-orm-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343) ~[spring-orm-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) ~[spring-orm-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) ~[spring-beans-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.2.4.RELEASE.jar!/:4.2.4.RELEASE] 
    ... 22 common frames omitted 
Caused by: java.lang.ClassNotFoundException: org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$Work 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_65] 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_65] 
    at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:178) ~[open-secret-0.0.1-SNAPSHOT.jar!/:na] 
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:142) ~[open-secret-0.0.1-SNAPSHOT.jar!/:na] 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_65] 
    ... 27 common frames omitted 

2016-02-05 20:44:13.958 WARN 7405 --- [   main] o.s.boot.SpringApplication    : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' is defined) 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl$Work 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1054) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:829) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) 
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113) 
    at com.opensecret.OpenSecretApplication.main(OpenSecretApplication.java:10) 
    ... 6 more 
Caused by: java.lang.NoClassDefFoundError: org/hibernate/boot/registry/classloading/internal/ClassLoaderServiceImpl$Work 
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:54) 
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) 
    ... 22 more 
Caused by: java.lang.ClassNotFoundException: org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$Work 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:178) 
    at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:142) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 27 more 

這是我的gradle這個文件:

buildscript { 
    ext { 
     springBootVersion = '1.3.1.RELEASE' 
     hibernateVersion = '5.0.7.Final' 
     mysqlVersion = '5.1.6' 
     junitVersion = '4.12' 
    } 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    } 
} 

apply plugin: 'spring-boot' 

jar { 
    baseName = 'open-secret' 
    version = '0.0.1-SNAPSHOT' 
} 

dependencies { 
    compile("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}") 
    compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 
    compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 
    compile("org.springframework.boot:spring-boot-starter-security:${springBootVersion}") 
    compile("org.hibernate:hibernate-core:${hibernateVersion}") 
    compile("mysql:mysql-connector-java:${mysqlVersion}") 
    testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}") 
    testCompile("junit:junit:${junitVersion}") 
    testCompile("org.mockito:mockito-core:1.10.19") 
} 


eclipse { 
    classpath { 
     containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER') 
     containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8' 
    } 
} 

這是我的控制器:

package com.opensecret.controller; 

    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.web.bind.annotation.*; 
    import com.opensecret.model.User; 
    import com.opensecret.repository.UserRepository; 

    import java.util.List; 

    @RestController 
    @RequestMapping(value="/api/user") 
    public class UserController { 

     @Autowired 
     private UserRepository userRepository; 

     @RequestMapping(method = RequestMethod.GET) 
     public List<User> findAll() { 
      return userRepository.findAll(); 
     } 

     @RequestMapping(method = RequestMethod.POST) 
     public User add(@RequestBody User user) { 
      User model = new User(); 
      model.setEmail(user.getEmail()); 
      model.setName(user.getEmail()); 
      return userRepository.saveAndFlush(model); 
     } 

     @RequestMapping(value = "/{id}", method = RequestMethod.GET) 
     public User findOne(@PathVariable int id) { 
      return userRepository.findOne(id); 
     } 

     @RequestMapping(value = "/{id}", method = RequestMethod.PUT) 
     public User update(@PathVariable int id, @RequestBody User user) { 
      User model = userRepository.findOne(id); 
      if (model != null) { 
       model.setName(user.getName()); 
       model.setEmail(user.getEmail()); 
       return userRepository.saveAndFlush(model); 
      } 
      return null; 
     } 

     @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) 
     public void delete(@PathVariable int id) { 
      userRepository.delete(id); 
     } 
    } 

This is my model: 
package com.opensecret.model; 

import javax.persistence.*; 
import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 

@Entity 
@Table(name="users") 
public class User { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "id") 
    private int id; 

    @NotNull 
    @Size(min = 3, max = 80) 
    @Column(name = "email") 
    private String email; 

    @NotNull 
    @Size(min = 2, max = 80) 
    @Column(name = "name") 
    private String name; 

    public int getId() { 
     return id; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

} 

這是我的安全配置:

package com.opensecret.config; 

import org.springframework.context.annotation.Configuration; 
import org.springframework.http.HttpMethod; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.config.http.SessionCreationPolicy; 

@Configuration 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .csrf().disable() 
       .authorizeRequests() 
       .antMatchers(HttpMethod.POST, "/api/**").authenticated() 
       .antMatchers(HttpMethod.PUT, "/api/**").authenticated() 
       .antMatchers(HttpMethod.DELETE, "/api/**").authenticated() 
       .anyRequest().permitAll() 
       .and() 
       .httpBasic().and() 
       .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 
    } 
} 

這是我的代表ository:

import com.opensecret.model.User; 
import org.springframework.data.jpa.repository.JpaRepository; 
import org.springframework.stereotype.Repository; 

@Repository 
public interface UserRepository extends JpaRepository<User,Integer>{ 
} 

這是application.properties:

server.port: 9000 
management.port: 9001 
management.address: 127.0.0.1 

spring.datasource.url= jdbc:mysql://localhost:3306/opensecret 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.datasource.username=root 
spring.datasource.password=password 

spring.jpa.hibernate.ddl-auto=create-drop 

這是主要的:

package com.opensecret; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class OpenSecretApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(OpenSecretApplication.class, args); 
    } 
} 

請幫助我的人,我無法理解爲什麼它的發生......

回答

2

作爲hibernateVersion b的結果,您已在類路徑中混合使用Hibernate版本eing設置爲5.0.7.Final和你spring-boot-starter-data-jpahibernate-core依賴關係:

compile("org.springframework.boot:spring-boot-starter-datajpa:${springBootVersion}") 
compile("org.hibernate:hibernate-core:${hibernateVersion}") 

spring-boot-starter-data-jpa取決於hibernate-entitymanager 4.3.11.Final,反過來,取決於hibernate-core。你已經覆蓋了後者的版本,所以你有hibernate-entitymanager 4.3.11混合hibernate-core 5.0.7.Final。

最簡單的解決方案是一個關於hibernate-entitymanager以取代hibernate-core你的依賴,使你的依賴是這樣的:

dependencies { 
    compile("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}") 
    compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 
    compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 
    compile("org.springframework.boot:spring-boot-starter-security:${springBootVersion}") 
    compile("org.hibernate:hibernate-entitymanager:${hibernateVersion}") 
    compile("mysql:mysql-connector-java:${mysqlVersion}") 
    testCompile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}") 
    testCompile("junit:junit:${junitVersion}") 
    testCompile("org.mockito:mockito-core:1.10.19") 
} 

我建議你拿東西了一步,雖然。 Spring Boot的Gradle插件爲其自身和多個第三方庫提供依賴關係管理,這意味着您在聲明依賴關係時不需要指定版本。如果利用這一點,並刪除一些依賴的是在傳遞地拉,可以簡化您的build.gradle這樣:

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE") 
    } 
} 

apply plugin: 'spring-boot' 
apply plugin: 'eclipse' 

jar { 
    baseName = 'open-secret' 
    version = '0.0.1-SNAPSHOT' 
} 

repositories { 
    mavenCentral() 
} 

ext['hibernate.version'] = '5.0.7.Final' 

dependencies { 
    compile("org.springframework.boot:spring-boot-starter-actuator") 
    compile("org.springframework.boot:spring-boot-starter-web") 
    compile("org.springframework.boot:spring-boot-starter-data-jpa") 
    compile("org.springframework.boot:spring-boot-starter-security") 
    compile("mysql:mysql-connector-java") 
    testCompile("org.springframework.boot:spring-boot-starter-test") 
} 


eclipse { 
    classpath { 
     containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER') 
     containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8' 
    } 
} 

需要注意的是被用來覆蓋春天啓動的默認休眠版本(4.3.11 ext['hibernate.version'] = '5.0.7.Final'。最後)到你想要的那個。

+0

非常感謝Andy Wilkinson先生。你告訴我的第一個用hibernate-entitymanager替換對hibernate-core的依賴不適合我。但第二個,我重寫Spring Boot的默認Hibernate版本(4.3.11.Final)以ext ['hibernate.version'] ='5.0.7.Final'對我來說工作正常....再次感謝您。 –

相關問題