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);
}
}
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.
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.
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.
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.
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)
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'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With