是否可以将Spring bean xml文件移到WAR之外?

时间:2015-06-02 13:22:12

标签: java spring war applicationcontext

我正在尝试通过调整web.xml来移动WAR之外的Spring Beans位置。我的web.xml包含:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/intu-1.0.0-beans.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

我想指定一个在WAR之外的路径。 我也尝试手动加载bean,但由于以下原因我没有得到它(这篇文章的其余部分是关于这个):我确实想要自动加载一些 bean。因为否则应用程序将无法启动。该项目是一个SOAP服务。我有另一个文件,rts-application-context.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:jaxws="http://cxf.apache.org/jaxws"
    xmlns:camel="http://camel.apache.org/schema/spring" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-camel.xml" />

    <jaxws:endpoint id="Intu"
        implementor="nl.billinghouse.intu.service.Intu"
        wsdlLocation="wsdl/intu.wsdl"
        endpointName="tns:IntuPort"
        serviceName="tns:Intu"
        xmlns:tns="http://intu"
        xmlns:ns1="http://www.skidata.com/projects/svp/parking/v4">
        <jaxws:features>
            <bean class="org.apache.cxf.feature.LoggingFeature" />
        </jaxws:features>
        <jaxws:properties>
            <entry key="exceptionMessageCauseEnabled" value="false" />
            <entry key="faultStackTraceEnabled" value="false" />
        </jaxws:properties> 
    </jaxws:endpoint>
</beans>

它将构造一个Intu,然后我将在Intu.java的构造函数中加载另一个beans文件。我有以下其他版本的web.xml:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/rts-application-context.xml</param-value>
  </context-param>
  <!--<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/intu-1.0.0-beans.xml</param-value>
  </context-param>-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

但是由于监听器它会自动尝试从WAR加载intu-1.0.0-beans.xml(我认为默认情况下)。当我删除该侦听器时,它也不会加载rts-application-context.xml,因此不会构造Intu。

编辑:我试图从WAR加载bean的错误,我希望它不会,是:

jun 02, 2015 3:55:57 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
jun 02, 2015 3:55:57 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/intu-1.0.0-beans.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/intu-1.0.0-beans.xml]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:349)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:92)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
    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:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1120)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1678)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/intu-1.0.0-beans.xml]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:117)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
    ... 26 more

1 个答案:

答案 0 :(得分:1)

您可以将file:路径与spring导入标记一起使用:

<import resource="file:/home/user/spring-beans.xml"/>

rts-application-context.xml