Spring with Castor - 初始化应用程序上下文时出现空指针异常

发布于 2024-08-25 01:30:12 字数 5113 浏览 14 评论 0原文

我正在尝试使用 spring 注册我的 Castor 映射文件,但似乎遇到了空指针异常。

在我的应用程序上下文中,我有:

   <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 和 Castor 1.3.1。

环顾四周,我发现我不是唯一一个遇到这个问题的人,但我似乎无法找到解决方案。

任何帮助将不胜感激。

I'm trying to register my castor mapping files with spring and I appear to be getting a null pointer exception.

In my application context I have:

   <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>

Where DistributionSamplerMappings.xml lives in the same dir as the application context.

I've tried using the spring-xml jar 1.2.1 and 1.5.3. but none of them seem to help.

The exception being thrown back is:

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

I'm using Spring 2.5.6 and Castor 1.3.1.

Looking around I find I'm not the only one who has had this problem, but I don't seem to be able to find a solution.

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戴着白色围巾的女孩 2024-09-01 01:30:12

首先,查看 XMLContextFactoryBean 是这里的最后一个。它表明映射资源以某种方式为空。这表明 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,或者其中包含已编译类的根目录的类文件夹。如果您使用的是 Eclipse,您可以通过将文件放入源文件夹中来完成此操作 - 它看起来有点凌乱,因为您宁愿在其他地方拥有配置信息,但至少它可以工作。

First, look at the code - line 118 of the XMLContextFactoryBean is the last one here. It suggests that somehow mappingResource is null. That suggests that getClass().getClassLoader().getResource(mappingLocation) is returning a null, so perhaps it can't find your file.

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

Now if you want the class loader to find a file, you need to put the file in the same place that it would look for classes. Putting your DistributionSamplerMappings.xml in the same directory as applicationContext isn't good enough. Try WEB-INF/classes, or whichever is the classes folder that has the root of your compiled classes inside it. If you're using Eclipse, you can do this by putting the file inside your source folder -- it looks a bit untidy, since you'd rather have config info elsewhere, but at least it will work.

穿越时光隧道 2024-09-01 01:30:12

如果映射类没有默认的公共构造函数,也可能会发生此异常。

This exception can also occur if a mapped class doesn't have a default public constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文