Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot load /WEB-INF/applicationContext.xml

Tags:

spring

Saw same issues in others queries, but did not find the answer, hence posting it afresh.

Created a sample Helloworld service in Rest and deployed it in Tomcat-Jersey. Not using Maven. Injecting a spring bean define in applicationContext.xml which is present in WEB-INF directory.

Deployed the service in Tomcat within Eclipse. But when I execute the rest service, cannot find file applicationContext.xml. Once I copy the file to WEB-INF/classes only, it works.

What is the way to make it work by keeping it only in WEB-INF?

web.xml :

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

Rest service code snippet :

    ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
like image 690
kakoli Avatar asked Sep 21 '26 10:09

kakoli


1 Answers

It seems reasonable to work when you drop the context descriptor under /WEB-INF/classes because this location is the root of classpath.

So try loading with below uri (without prefix) if you want to drop your applicationContext.xml under WEB-INF folder:

    ctx = new ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml");

Otherwise, can you explain why are you trying to load your applicationContext.xml within your web application?

Alternatively, you can load your application context programatically as follows:

ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

BR.

like image 184
tmarwen Avatar answered Sep 23 '26 05:09

tmarwen