9159金沙游艺场


9159金沙游艺场HTML
图片 14
在微服务领域Spring Boot自动伸缩如何实现

java-spring boot 打包后运行报错,求解

最近排查一个spring boot应用抛出hibernate.validator
NoClassDefFoundError的问题,异常信息如下:

IntelliJ使用springboot2.0版本以上报错,2.0以下就能正常启动 10C
报错如下
Error starting ApplicationContext. To display the conditions report
re-run your application with ‘debug’ enabled.
2018-08-28 19:29:13.156 ERROR 5532 — [ main]
o.s.boot.SpringApplication : Application run failed

spring boot 打包后运行报错,求解
…]

Caused by: java.lang.NoClassDefFoundError: Could not initialize class
org.hibernate.validator.internal.engine.ConfigurationImpl at
org.hibernate.validator.HibernateValidator.createGenericConfiguration(HibernateValidator.java:33)
~[hibernate-validator-5.3.5.Final.jar:5.3.5.Final] at
javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:276)
~[validation-api-1.1.0.Final.jar:na] at
org.springframework.boot.validation.MessageInterpolatorFactory.getObject(MessageInterpolatorFactory.java:53)
~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE] at
org.springframework.boot.autoconfigure.validation.DefaultValidatorConfiguration.defaultValidator(DefaultValidatorConfiguration.java:43)
~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
atsun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
~[na:1.8.0_112]
atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
~[na:1.8.0_112]
atsun.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.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] …32common frames
omitted

org.springframework.beans.factory.BeanCreationException: Error creating
bean with name ‘defaultValidator’ defined in class path resource
[org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]:
Invocation of init method failed; nested exception is
java.lang.NoClassDefFoundError: Could not initialize class
org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at
org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)
[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1258)
[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at
com.cheyr.management.ManagementApplication.main(ManagementApplication.java:10)
[classes/:na]
Caused by: java.lang.NoClassDefFoundError: Could not initialize class
org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager
at
org.hibernate.validator.internal.engine.ValidatorFactoryImpl.(ValidatorFactoryImpl.java:140)
~[hibernate-validator-6.0.11.Final.jar:6.0.11.Final]
at
org.hibernate.validator.HibernateValidator.buildValidatorFactory(HibernateValidator.java:38)
~[hibernate-validator-6.0.11.Final.jar:6.0.11.Final]
at
org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:364)
~[hibernate-validator-6.0.11.Final.jar:6.0.11.Final]
at
org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:309)
~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695)
~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]
… 16 common frames omitted

2017-11-01 17:39:03.057 INFO 3598 — [ main] org.hibernate.Version :
HHH000412: Hibernate Core {5.0.12.Final}
2017-11-01 17:39:03.059 INFO 3598 — [ main]
org.hibernate.cfg.Environment : HHH000206: hibernate.properties not
found
2017-11-01 17:39:03.060 INFO 3598 — [ main]
org.hibernate.cfg.Environment : HHH000021: Bytecode provider name :
javassist
2017-11-01 17:39:03.116 WARN 3598 — [ 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: javax/xml/bind/JAXBException
2017-11-01 17:39:03.118 INFO 3598 — [ main]
o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2017-11-01 17:39:03.164 INFO 3598 — [ main]
utoConfigurationReportLoggingInitializer :

这个错误信息表面上是 NoClassDefFoundError ,但是实际上 ConfigurationImpl
这个类是在 hibernate-validator-5.3.5.Final.jar
里的,不应该出现找不到类的情况。

Disconnected from the target VM, address: ‘127.0.0.1:60027’, transport:
‘socket’

Error starting ApplicationContext. To display the auto-configuration
report re-run your application with ‘debug’ enabled.
2017-11-01 17:39:03.171 ERROR 3598 — [ main]
o.s.boot.SpringApplication : Application startup failed

那为什么应用里抛出这个 NoClassDefFoundError ?

启动文件内容如下,没有做任何更改,纯新项目
@SpringBootApplication
public class ManagementApplication {

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: javax/xml/bind/JAXBException
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078)
~[spring-context-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857)
~[spring-context-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
~[spring-context-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
~[spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
at
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
[spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
at
org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
[spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
[spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
[spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
[spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
at com.xzzx.small.SmallApplication.main(SmallApplication.java:16)
[classes!/:0.0.1-SNAPSHOT]
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) ~[na:na]
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
~[na:na]
at
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
~[na:na]
at
org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
[small-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
[small-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
[small-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
[small-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: java.lang.NoClassDefFoundError:
javax/xml/bind/JAXBException
at
org.hibernate.boot.spi.XmlMappingBinderAccess.(XmlMappingBinderAccess.java:43)
~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at org.hibernate.boot.MetadataSources.(MetadataSources.java:87)
~[hibernate-core-5.0.12.Final.jar!/:5.0.12.Final]
at
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(EntityManagerFactoryBuilderImpl.java:179)
~[hibernate-entitymanager-5.0.12.Final.jar!/:5.0.12.Final]
at
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.(EntityManagerFactoryBuilderImpl.java:149)
~[hibernate-entitymanager-5.0.12.Final.jar!/:5.0.12.Final]
at
org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:54)
~[spring-orm-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
~[spring-orm-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370)
~[spring-orm-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359)
~[spring-orm-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
~[spring-beans-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
… 24 common frames omitted
Caused by: java.lang.ClassNotFoundException:
javax.xml.bind.JAXBException
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466)
~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:563)
~[na:na]
at
org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:94)
~[small-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
~[na:na]
… 34 common frames omitted

有经验的开发人员从 Could not initialize class
这个信息就可以知道,实际上是一个类在初始化时抛出的异常,比如static的静态代码块,或者static字段初始化的异常。

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

谁初始化了 org.hibernate.validator.internal.engine.ConfigurationImpl

}

但是当我们在 HibernateValidator 这个类,创建 ConfigurationImpl
的代码块里打断点时,发现有两个线程触发了断点:

publicclassHibernateValidatorimplementsValidationProvider{
@OverridepublicConfiguration<?>
createGenericConfiguration(BootstrapState state)
{returnnewConfigurationImpl; }

其中一个线程的调用栈是:

Thread [background-preinit] (Classload:ConfigurationImpl)
HibernateValidator.createGenericConfiguration(BootstrapState)line:33Validation$GenericBootstrapImpl.configure()line:276BackgroundPreinitializer$ValidationInitializer.run()line:107BackgroundPreinitializer$1.runSafelyline:59BackgroundPreinitializer$1.run()line:52Thread.run()line:745

另外一个线程调用栈是:

Thread [main] (Suspended (breakpoint at line33inHibernateValidator))
owns: ConcurrentHashMap owns:Object
HibernateValidator.createGenericConfiguration(BootstrapState)
line:33Validation$GenericBootstrapImpl.configure()
line:276MessageInterpolatorFactory.getObject()
line:53DefaultValidatorConfiguration.defaultValidator()
line:43NativeMethodAccessorImpl.invoke0(Method,Object,Object[])
line:notavailable [native method]
NativeMethodAccessorImpl.invoke(Object,Object[])
line:62DelegatingMethodAccessorImpl.invoke(Object,Object[])
line:43Method.invoke(Object,Object…)
line:498CglibSubclassingInstantiationStrategy(SimpleInstantiationStrategy).instantiate(RootBeanDefinition,String,
BeanFactory,Object, Method,Object…)
line:162ConstructorResolver.instantiateUsingFactoryMethod(String,
RootBeanDefinition,Object[])
line:588DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).instantiateUsingFactoryMethod(String,
RootBeanDefinition,Object[]) line:1173

显然,这个线程的调用栈是常见的spring的初始化过程。

BackgroundPreinitializer 做了什么

那么重点来看下 BackgroundPreinitializer 线程做了哪些事情:

@Order(LoggingApplicationListener.DEFAULT_ORDER
+1)publicclassBackgroundPreinitializerimplementsApplicationListener{@OverridepublicvoidonApplicationEvent(ApplicationEnvironmentPreparedEvent
event){try{ Thread thread =newThread(newRunnable()
{@Overridepublicvoidrun(){ runSafely(newMessageConverterInitializer;
runSafely(newMBeanFactoryInitializer;
runSafely(newValidationInitializer; runSafely(newJacksonInitializer;
runSafely(newConversionServiceInitializer; }publicvoidrunSafely(Runnable
runnable){try{ runnable.run(); }catch(Throwable ex) {// Ignore} }
},”background-preinit”); thread.start(); }

可以看到 BackgroundPreinitializer 类是spring
boot为了加速应用的初始化,以一个独立的线程来加载hibernate
validator这些组件。

这个 background-preinit 线程会吞掉所有的异常。

显然 ConfigurationImpl
初始化的异常也被吞掉了,那么如何才能获取到最原始的信息?

获取到最原始的异常信息

在 BackgroundPreinitializer 的 run() 函数里打一个断点(注意是 Suspend
thread 类型, 不是 Suspend VM ),让它先不要触发 ConfigurationImpl
的加载,让spring boot的正常流程去触发 ConfigurationImpl
的加载,就可以知道具体的信息了。

那么打出来的异常信息是:

相关文章

No Comments, Be The First!
近期评论
    功能
    网站地图xml地图