By "local filename" I mean that resource file is located in the same directory as class file. In the case below this is JUnitRunner.class
file. Java's getResource()
file can handle this if path does not start with /
'
I can't figure out, how to do the same ClassPathXmlApplicationContext
constructor?
package springtests;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JUnitRunner {
private final static Logger log = LoggerFactory.getLogger(JUnitRunner.class);
@Test
public void test() throws URISyntaxException {
String filename = "test01.xml";
URL url = getClass().getResource(filename);
File file = new File(url.toURI());
log.info("File exists: {}", file.exists());
try {
new ClassPathXmlApplicationContext(filename);
}
catch(Exception e) {
log.error("Can't load context", e);
}
}
}
the output follows
15:32:27,375 3 [main] INFO springtests.JUnitRunner - File exists: true 15:32:27,422 50 [main] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ee3aa7: startup date [Thu Nov 01 15:32:27 MSK 2012]; root of context hierarchy 15:32:27,475 103 [main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [test01.xml] 15:32:27,477 105 [main] ERROR springtests.JUnitRunner - Can't load context org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [test01.xml]; nested exception is java.io.FileNotFoundException: class path resource [test01.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) ...
UPDATE
XML file is located in the same folder as class file. It is visible from the fact that getResource()
see it.
so, your xml file in package springtests
and correct creation of AppContext should be
new ClassPathXmlApplicationContext("springtests/test01.xml");
I don't properly understand what is that you're asking, but have you tried:
new ClassPathXmlApplicationContext("classpath*:test01.xml");
That will search in all the classpath for test01.xml
. You can read more about this in the Spring resources documentation page.
It might need to configure DocumentBuilderFactory
. Reference
Example :
@BeforeClass
public static void init() {
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
}
It is possible to load an applicationcontext from a location relative to the current Class.
This is all you need to do to make it happen:
URL resourceUrl = JUnitRunner.class.getResource("test01.xml");
ApplicationContext applicationContext = new GenericXmlApplicationContext(
new UrlResource(resourceUrl));
来源:https://stackoverflow.com/questions/13177288/how-to-pass-local-filename-to-classpathxmlapplicationcontext