xml文件绝对路径
这种很简单绝对路径只要没错idea就直接可以显示它的路径,构建的是configLocations
java
public ClassPathXmlApplicationContext(String... configLocations)
xml相对于,基准class的路径
只有这种的会构建configResources
java
public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz)
public ClassPathXmlApplicationContext(String path, Class<?> clazz)
String[] configFiles = {
"innerbeantest.xml"
};
ClassPathXmlApplicationContext cpa = new ClassPathXmlApplicationContext(configFiles, WebConfig.class);
xml配置文件相对于WebConfig类的位置。此时关键还要看构建工具配置,如果XML在java目录下需要额外配置,否则打包不会把java目录下资源文件复制过去
xml
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include> <!-- 包含XML文件 --> </includes>
</resource>
<!-- 如果XML在java目录下需要额外配置 -->
<resource>
<directory>src/main/java</directory>
<includes> <include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
加载bean定义时
先加载configResources,再加载configLocations
java
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
Resource[] configResources = getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
String[] configLocations = getConfigLocations();
if (configLocations != null) {
reader.loadBeanDefinitions(configLocations);
}
}
我们还是建议直接使用configLocations,除非配置资源专供某一个类,或包使用,为了容易体现他们之间关系,使用configResources。