Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right path to applicationContext.xml using ClassPathXmlApplicationContext

I have working web-application with applicationContext.xml in WEB-INF/config/applicationContext.xml. Now i need to implement some testing tool as standalone application, that can use the same applicationContext.xml, but have problem with path to config for ClassPathXmlApplicationContext class.

I know that when i copy applicationContext.xml to default package (where .java resides) i can use ClassPathXmlApplicationContext("applicationContext.xml"), but is this necessary ?

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AdminTool {

    private Logger log = Logger.getLogger(getClass());

    /** all below doesn't work - FileNotFoundException) **/
    private static final ApplicationContext ac = new ClassPathXmlApplicationContext("/WEB-INF/config/applicationContext.xml");
    private static final ApplicationContext ac = new ClassPathXmlApplicationContext("config/applicationContext.xml");
    private static final ApplicationContext ac = new ClassPathXmlApplicationContext("../config/applicationContext.xml");

    public static void main(String[] args) {
        new AdminTool();
    }

    public AdminTool() {
        log.debug(ac);
    }

}
like image 340
marioosh Avatar asked Jan 26 '11 09:01

marioosh


People also ask

Where should ApplicationContext XML be placed?

It needs to be in the classpath. You can put the original editable instance anywhere (e.g. a config directory off the root) but then you will need to have your build management tool (e.g. Ant or Maven ) copy it into the classpath for the runtime.

Where is ApplicationContext XML in Spring?

When the application starts, the Spring loads this configuration file and uses it to create a WebApplicationContext object. In the absence of contextConfigLocation, by default, the system will look for/WEB-INF/applicationContext. xml to load. In short, applicationContext is the central interface in Spring.

What is ApplicationContext XML file used for?

Applicationcontext. xml - It is standard spring context file which contains all beans and the configuration that are common among all the servlets. It is optional file in case of web app. Spring uses ContextLoaderListener to load this file in case of web application.

What is the path that will have the file that contains XML configuration metadata?

xml file will be kept in the WebContent/WEB-INF directory of your web application. Upon initialization of HelloWeb DispatcherServlet, the framework will try to load the application context from a file named [servlet-name]-servlet. xml located in the application's WebContent/WEB-INF directory.


2 Answers

In a local test scenario, you are not inside a Jar, so you don't need Classpath-based access. Use FileSystemXmlApplicationContext instead.

Probably something like this:

private static final ApplicationContext ac =
    new FileSystemXmlApplicationContext(
        "src/WEB-INF/config/applicationContext.xml"
    );

(paths are relative from the execution directory)

like image 137
Sean Patrick Floyd Avatar answered Oct 24 '22 02:10

Sean Patrick Floyd


This is the reason why I prefer to put all configuration files in classes folder. In fact, using Maven, you can put all these files in src/main/resources. Then, in your test files you can access as 'classpath:applicationContext.xml'.

like image 38
sinuhepop Avatar answered Oct 24 '22 02:10

sinuhepop