[英]Loading spring XML from location different from classpath
I am trying to read spring xml using ClassPathXmlApplicationContext class as below. 我正在尝试使用ClassPathXmlApplicationContext类读取spring xml,如下所示。
ApplicationContext context = new ClassPathXmlApplicationContext("file:../WebContent/WEB-INF/dispatcher-servlet.xml");
Service service = (Service ) context.getBean("service");
But I am getting FileNotFound exception. 但是我收到FileNotFound异常。 dispatcher-servlet.xml is located under WebContent/WEB-INF/dispatcher-servlet.xml. dispatcher-servlet.xml位于WebContent / WEB-INF / dispatcher-servlet.xml下。 If I move the file to Src folder, it works fine. 如果我将文件移到Src文件夹,则可以正常工作。
I tried different ways like 我尝试了不同的方式
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:../WebContent/WEB-INF/dispatcher-servlet.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("/WEB-INF/dispatcher-servlet.xml");
But these don't work. 但是这些不起作用。 Can someone provide some inputs. 有人可以提供一些输入吗?
从Spring文档中 :
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");
Try this in your web.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>
Or use ServletContextResource
if you need to do this in code. 或者,如果需要在代码中执行此操作,请使用ServletContextResource
。 See here . 看这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.