繁体   English   中英

从不同于classpath的位置加载spring XML

[英]Loading spring XML from location different from classpath

我正在尝试使用ClassPathXmlApplicationContext类读取spring xml,如下所示。

    ApplicationContext context = new ClassPathXmlApplicationContext("file:../WebContent/WEB-INF/dispatcher-servlet.xml");
    Service service = (Service ) context.getBean("service");

但是我收到FileNotFound异常。 dispatcher-servlet.xml位于WebContent / WEB-INF / dispatcher-servlet.xml下。 如果我将文件移到Src文件夹,则可以正常工作。

我尝试了不同的方式

    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:../WebContent/WEB-INF/dispatcher-servlet.xml");

 ApplicationContext context = new ClassPathXmlApplicationContext("/WEB-INF/dispatcher-servlet.xml");

但是这些不起作用。 有人可以提供一些输入吗?

Spring文档中

ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");

在您的web.xml中尝试以下操作:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

或者,如果需要在代码中执行此操作,请使用ServletContextResource 这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM