简体   繁体   English

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

[英]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.

相关问题 从不同于classpath的位置加载spring XML以在Servlet中获取bean - Loading spring XML from location different from classpath to get a bean in Servlet 春天-从类路径加载GenericXmlApplicationContext - spring - GenericXmlApplicationContext loading from classpath 从 Spring 引导中的类路径加载嵌套资源 - Loading nested resource from the classpath in Spring Boot Spring Boot无法从位置&#39;classpath:/application.xml&#39;加载属性源 - Spring Boot failed to load property source from location 'classpath:/application.xml' 即使从路径中的文件,也无法从URL位置[classpath:spring / spring-persistence-layer.xml]导入bean定义 - Failed to import bean definitions from URL location [classpath:spring/spring-persistence-layer.xml] even when the file in the path 从类路径加载DLL - Loading DLL from Classpath 从类路径加载jar的context.xml - Loading a jar's context.xml from classpath 基于Spring Java的配置加载资源(来自类路径) - Spring Java-based configuration loading resources (from classpath) Spring引导嵌入式tomcat不从类路径加载SSL密钥库文件 - Spring boot embedded tomcat not loading SSL keystore file from classpath spring - 来自classpath资源的hibernate load * .hbm.xml - spring - hibernate load *.hbm.xml from classpath resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM