ClassPathXmlApplicationContext资源加载

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。

相关推荐
狼爷1 天前
Java 开发必看:UUIDv7 终于标准化了!这次真香🔥(基于 uuid-creator 实战)
java
努力搬砖的咸鱼1 天前
Node.js 和 Java 项目怎么写 Dockerfile
java·开发语言·docker·云原生·容器·node.js
阿无,1 天前
Java设计模式之装饰者模式
java·开发语言·设计模式
盼哥PyAI实验室1 天前
序列的力量——Python 内置方法的魔法解密
java·前端·python
zl9798991 天前
SpringBoot-常用注解
java·spring boot·spring
午安~婉1 天前
硅谷甄选(续2)首页
java·前端·javascript
掉鱼的猫1 天前
全球首个支持 IETF JSONPath (RFC 9535) 标准的 Java 框架,Snack4-Jsonpath v4.0.0 发布
java·json
赶飞机偏偏下雨1 天前
【Java笔记】消息队列
java·开发语言·笔记
yacolex1 天前
Mac安装使用Gradle
spring·macos·gradle
豐儀麟阁贵1 天前
2.6 代码注释与编码规
java·开发语言