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。

相关推荐
未若君雅裁几秒前
算法复杂度与数据结构:Java 集合篇的第一块基石
java·数据结构·算法
致Great9 分钟前
Claude Code 上线 Dynamic Workflows:一句话调度 1000 个子智能体并行干活
java·linux·服务器
一个做软件开发的牛马11 分钟前
Java 常用类:String不可变、新时间API与包装类陷阱
java·后端
yurenpai(27届找实习中)22 分钟前
redis_点评(25.附件店铺—把数据库里的店铺按【类型分组】,批量导入Redis 的 GEO 地理位置结构)
java·redis·缓存
云烟成雨TD31 分钟前
Spring AI Alibaba 1.x 系列【66】Graph 长期记忆
java·人工智能·spring
Javatutouhouduan1 小时前
Java面试大厂真题汇总!
java·java面试·java面试题·后端开发·java编程·java架构师·java八股文
maomao大哥闯天下1 小时前
K8s对象deployment、job、service应用详解
java·容器·kubernetes
闪电悠米1 小时前
黑马点评-优惠券秒杀-05_local_lock_cluster_problem
java·spring boot·redis·缓存
IronMurphy1 小时前
SSM拷打第二讲!!!
java·spring·mybatis
小江的记录本1 小时前
【JVM虚拟机】类加载机制:类加载全流程:加载→验证→准备→解析→初始化(附《思维导图》+《面试高频考点清单》)
java·jvm·spring boot·算法·安全·spring·面试