歡迎訪問昆山寶鼎軟件有限公司網站! 設為首頁 | 網站地圖 | XML | RSS訂閱 | 寶鼎郵箱 | 后臺管理
?

新聞資訊

MENU

軟件開發知識

// loaded and inserted in cl 昆山軟件開發 ass hierarchy (but not

點擊: 次  來源:寶鼎軟件 時間:2017-06-19

原文出處: hengyunabc

問題

可重現的Demo代碼:demo.zip
最近排查一個spring boot應用拋出hibernate.validator NoClassDefFoundError的問題,異常信息如下:

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]
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.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
... 32 common frames omitted

這個錯誤信息外貌上是NoClassDefFoundError,可是實際上ConfigurationImpl這個類是在hibernate-validator-5.3.5.Final.jar里的,不該該呈現找不到類的環境。

那為什么應用里拋出這個NoClassDefFoundError ?

有履歷的開拓人員從Could not initialize class 這個信息就可以知道,實際上是一個類在初始化時拋出的異常,好比static的靜態代碼塊,可能static字段初始化的異常。

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

可是當我們在HibernateValidator 這個類,建設ConfigurationImpl的代碼塊里打斷點時,發明有兩個線程觸發了斷點:

public class HibernateValidator implements ValidationProvider<HibernateValidatorConfiguration> {
	@Override
	public Configuration<?> createGenericConfiguration(BootstrapState state) {
		return new ConfigurationImpl( state );
	}

個中一個線程的挪用棧是:

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

別的一個線程挪用棧是:

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

顯然,這個線程的挪用棧是常見的spring的初始化進程。

BackgroundPreinitializer 做了什么

那么重點來看下 BackgroundPreinitializer 線程做了哪些工作:

@Order(LoggingApplicationListener.DEFAULT_ORDER + 1)
public class BackgroundPreinitializer
		implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
	@Override
	public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
		try {
			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					runSafely(new MessageConverterInitializer());
					runSafely(new MBeanFactoryInitializer());
					runSafely(new ValidationInitializer());
					runSafely(new JacksonInitializer());
					runSafely(new ConversionServiceInitializer());
				}
				public void runSafely(Runnable runnable) {
					try {
						runnable.run();
					}
					catch (Throwable ex) {
						// Ignore
					}
				}
			}, "background-preinit");
			thread.start();
		}

可以看到BackgroundPreinitializer類是spring boot為了加快應用的初始化,軟件開發,以一個獨立的線程來加載hibernate validator這些組件。

這個 background-preinit 線程會吞掉所有的異常。
顯然ConfigurationImpl 初始化的異常也被吞掉了,那么如何才氣獲取到最原始的信息?

獲取到最原始的異常信息

排列三305组选前后关系 5分快3和值走势图怎么看 辽宁11选5手机版规则 安徽十一选五任五遗漏导航 快乐十分任选4玩法 dota2博彩 北京快3预测 hr娱乐怎么登录不上去了 基金配资比例 青11选5开奖结果 大乐透规则