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。

相关推荐
代码栈上的思考37 分钟前
SpringBoot 拦截器
java·spring boot·spring
送秋三十五41 分钟前
一次大文件处理性能优化实录————Java 优化过程
java·开发语言·性能优化
雨中飘荡的记忆43 分钟前
千万级数据秒级对账!银行日终批处理对账系统从理论到实战
java
jbtianci1 小时前
Spring Boot管理用户数据
java·spring boot·后端
Sylvia-girl1 小时前
线程池~~
java·开发语言
魔力军1 小时前
Rust学习Day3: 3个小demo实现
java·学习·rust
时艰.1 小时前
java性能调优 — 高并发缓存一致性
java·开发语言·缓存
落花流水 丶1 小时前
Java 多线程完全指南
java
那我掉的头发算什么1 小时前
【Mybatis】Mybatis-plus使用介绍
服务器·数据库·后端·spring·mybatis
jxy99981 小时前
mac mini 安装java JDK 17
java·开发语言·macos