How to pass local filename to ClassPathXmlApplicationContext?

耗尽温柔 提交于 2019-12-04 09:52:04

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");
}
stefaan dutry

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!