尝试使用 Spring 。 xml文件在src下。我已经搜索过,但找不到问题所在。似乎找不到 xml 文件。
我收到以下错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.springframework.core.env.AbstractEnvironment.suppressGetenvAccess(AbstractEnvironment.java:406)
at org.springframework.core.env.AbstractEnvironment.getSystemEnvironment(AbstractEnvironment.java:368)
at org.springframework.core.env.StandardEnvironment.customizePropertySources(StandardEnvironment.java:79)
at org.springframework.core.env.AbstractEnvironment.<init>(AbstractEnvironment.java:124)
at org.springframework.core.env.StandardEnvironment.<init>(StandardEnvironment.java:54)
at org.springframework.context.support.AbstractApplicationContext.createEnvironment(AbstractApplicationContext.java:444)
at org.springframework.context.support.AbstractApplicationContext.getEnvironment(AbstractApplicationContext.java:283)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.resolvePath(AbstractRefreshableConfigApplicationContext.java:122)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.setConfigLocations(AbstractRefreshableConfigApplicationContext.java:80)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:137)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.hancock.javabrains.DrawingApp.main(DrawingApp.java:13)
Caused by: java.lang.NullPointerException
at org.springframework.core.SpringProperties.<clinit>(SpringProperties.java:58)
... 12 more
这是导致错误的行(通过将 Beans.xml 更改为错误的名称,我仍然会遇到同样的错误):
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
xml (Beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="helloWorld" class="org.hancock.javabrains.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
最佳答案
尝试给出完整的文件路径
ApplicationContext ctx = new FileSystemXmlApplicationContext("Beans.xml");
关于java - Spring ClassPathXmlApplicationContext 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22864931/