9

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);
    }

}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
marioosh
  • 27,328
  • 49
  • 143
  • 192

5 Answers5

17

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)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • I was thinking about running my AdminTool using webapp.war. Will it be possible using FileSystemXmlApplicationContext ? – marioosh Jan 26 '11 at 09:41
  • @marioosh No, in a war you have the same problems as in a jar: there are no files, just entries. Is the war deployed to a servlet container or local? – Sean Patrick Floyd Jan 26 '11 at 09:53
  • @Patrick I would like to use AdminTool class from .war without deploying. PS: I have maven project: new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/config/applicationContext.xml"); works when i run AdminTool in Eclipse – marioosh Jan 26 '11 at 10:03
3

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'.

sinuhepop
  • 20,010
  • 17
  • 72
  • 107
1
private static final ApplicationContext ac =
    new FileSystemXmlApplicationContext(
        "/WEB-INF/config/applicationContext.xml"
    );
jmj
  • 237,923
  • 42
  • 401
  • 438
0
private static final ApplicationContext ac =
new FileSystemXmlApplicationContext(
    "/WEB-INF/config/applicationContext.xml"
);
Nazik
  • 8,696
  • 27
  • 77
  • 123
Dusmant
  • 1
  • 2
-1

please go to property>Java Build Path>Source of your project check the classpath and add the directory before WEB-INF in the classpath. definitely it's work

Milind
  • 1