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。

相关推荐
古法安卓2 小时前
Android-显示流程
android·java·android studio
l1t3 小时前
DeepSeek总结的DuckDB 如何更快地运行递归 CTE
java·开发语言·数据库·mysql·duckdb
小手cool3 小时前
使用递归对数组进行反转操作
java·数据结构·算法
MetaLite3 小时前
SpringBoot分页接口怎么设计-pageSize不设上限会发生什么
java·spring boot·后端
晚安code3 小时前
LangChain4j AiService 实战:会话记忆与结构化输出
java·langchain
晚安code4 小时前
LangChain4j 实战:RAG、工具调用、护轨与 SSE 流式输出
java·langchain
郑州光合科技余经理4 小时前
本地生活平台搭建:统一订单表与多后台切换怎么拆
java·开发语言·前端·系统架构·uni-app·php·ai编程
让你三行代码QAQ4 小时前
SpringAI-Advisors
spring·ai编程
杰哥哥不是个好叔叔5 小时前
【Spring AI】Spring AI 2.0.1 新特性袭来
java·人工智能·spring
莫陌尛.5 小时前
Java核心知识点:直接内存、CPU密集型线程池、并行流对比文档
java·开发语言