2010-03-14 82 views
2

我試圖用spring註冊我的castor映射文件,而且我似乎得到一個空指針異常。Spring with Castor - 空指針初始化時出現異常Application Context

在我的應用程序方面,我有:

<bean id="xmlContext" class="org.castor.spring.xml.XMLContextFactoryBean"> 
    <property name="mappingLocations"> 
     <list> 
      <value>DistributionSamplerMappings.xml</value> 
     </list> 
    </property> 
    <property name="castorProperties"> 
      <props> 
       <prop key="org.exolab.castor.xml.strictelements">false</prop> 
      </props> 
     </property> 
</bean> 

<bean id="marshaller" 
     class="org.castor.spring.xml.CastorMarshallerFactoryBean"> 
    <property name="xmlContext"><ref local="xmlContext"/></property> 
</bean> 

<bean id="unmarshaller" 
     class="org.castor.spring.xml.CastorUnmarshallerFactoryBean"> 
     <property name="xmlContext"> <ref local="xmlContext"/></property> 
     <property name="ignoreExtraElements"><value>true</value></property> 
     <property name="ignoreExtraAttributes"><value>true</value></property> 
    </bean> 

凡DistributionSamplerMappings.xml住在同一目錄作爲應用程序上下文。

我試過使用spring-xml jar 1.2.1和1.5.3。但他們似乎都沒有幫助。

的異常被拋出的回覆是:

SEVERE: Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xmlContext' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) 
at java.security.AccessController.doPrivileged(Native Method) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) 
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255) 
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199) 
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45) 
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3830) 
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4337) 
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) 
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719) 
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) 
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) 
at org.apache.catalina.core.StandardService.start(StandardService.java:516) 
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) 
at org.apache.catalina.startup.Catalina.start(Catalina.java:566) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) 
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) 
Caused by: java.lang.NullPointerException 
at org.castor.spring.xml.XMLContextFactoryBean.afterPropertiesSet(XMLContextFactoryBean.java:118) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335) 
... 30 more 

我使用Spring 2.5.6和1.3.1蓖麻。

環顧四周我發現我不是唯一一個遇到過這個問題的人,但我似乎無法找到解決方案。

任何幫助將不勝感激。

回答

3

首先,看看XMLContextFactoryBean的代碼行118是最後一個。它表明mappingResource以某種方式爲空。這表明getClass()。getClassLoader()。getResource(mappingLocation)返回null,所以它可能找不到你的文件。

   mappingLocation = (String) iter.next(); 
       URL mappingResource = getClass().getClassLoader() 
         .getResource(mappingLocation); 
       mapping.loadMapping(new InputSource(mappingResource 
         .openStream())); // NPE occurs on this line. 

現在如果你想在加載程序才能找到一個文件,你需要把該文件中,它會尋找同一個地方。將你的DistributionSamplerMappings.xml放在與applicationContext相同的目錄中不夠好。請嘗試WEB-INF/classes,或者其中包含已編譯類的根目錄的classes文件夾。如果你使用的是Eclipse,你可以通過將文件放入你的源文件夾來做到這一點 - 它看起來有點不整潔,因爲你寧願在別處配置信息,但至少它會起作用。

+0

我曾嘗試過,但我意識到我的錯誤是我說src/my/classpath/file.xml時,我應該說我的/ classpath/file.xml 謝謝! – Babyangle86 2010-03-14 13:32:00

0

如果映射類沒有默認的公共構造函數,也會發生此異常。