![](/img/trans.png)
[英]Getting Error “Exception in thread ”main“ java.lang.ExceptionInInitializerError”
[英]Spring Error- Exception in thread “main” java.lang.ExceptionInInitializerError
作為Java應用程序運行時,在線程“ main” java.lang.ExceptionInInitializerError中出現錯誤Exception
這是針對Spring的,我在其中創建了一個SpringConfig.xml文件,該文件使用ApplicationContext接口從TestSpringProject類調用。 我沒有使用過和依賴工具..只是導入了所需的彈簧罐
TestSpringProject.java:
public class TestSpringProject {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
Restaurant res = (Restaurant) context.getBean("restaurantBean");
res.greetCustomer();
}
}
SpringConfig.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="restaurantBean" class="com.packages.Restaurant.java">
</bean>
</beans>
Restaurant.java:
public class Restaurant {
public void greetCustomer() {
System.out.println("Welcome Customer");
}
}
預期結果:歡迎客戶
實際結果:
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:160)
at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:230)
at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:92)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:59)
at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:62)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:141)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.packages.TestSpringProject.main(TestSpringProject.java:11)
Caused by: java.lang.NullPointerException
at org.apache.commons.logging.LogFactory.<clinit>(LogFactory.java:71)
我認為問題的根本原因在於您自己說的一句話:
我沒有使用過和依賴工具..只是導入了所需的彈簧罐
Spring本身依賴第三方庫,例如commons-logging。 通常,依賴項管理工具(從這個角度來看像maven)具有傳遞依賴項的概念,這意味着,如果您的項目依賴於spring並且spring依賴於commons日志記錄,那么您實際上將依賴於common日志記錄,這意味着您將無法即使您的代碼不依賴於commons-logging本身也可以運行該項目。
因此,您至少應導入此公用日志記錄罐,但是,我確實建議您使用一些可以解決這些傳遞性依賴關系的構建工具來管理項目,因為即使您要解決此問題,也會有類似的問題。
您的代碼工作正常,依賴關系(jar)可能存在一些問題。 您可以嘗試從此處下載最新版本-https: //repo.spring.io/release/org/springframework/spring/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.