You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中如何读取XML属性文件中的属性

别发愁,我来一步步教你在Spring Boot里搞定从XML属性文件读取SQL的操作!

步骤1:确认你的XML属性文件配置

首先,确保你的XML属性文件(比如命名为sql-queries.xml)放在src/main/resources目录下,格式保持你原来的样子就行,比如:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="sample.select">
        <![CDATA[
            SELECT id, name, email 
            FROM users 
            WHERE active = true
        ]]>
    </entry>
</properties>

CDATA块会完整保留你的SQL格式,不用担心换行或特殊字符被转义。

步骤2:让Spring加载这个XML属性文件

Spring Boot默认不会自动识别XML格式的properties文件,所以我们需要写个配置类来手动加载它:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import java.util.Properties;

@Configuration
public class SqlPropertiesConfig {

    // 加载XML属性文件并转为Properties对象
    @Bean(name = "sqlQueries")
    public Properties sqlQueries() throws Exception {
        PropertiesFactoryBean factoryBean = new PropertiesFactoryBean();
        factoryBean.setLocation(new ClassPathResource("sql-queries.xml"));
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }

    // 配置占位符解析器,让@Value能识别属性
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() throws Exception {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setProperties(sqlQueries());
        return configurer;
    }
}

步骤3:在DAO中获取SQL属性

这里有两种简单的方式:

方式一:直接注入单个SQL(推荐,更简洁)

用@Value注解直接把sample.select属性注入到DAO的变量中:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao {

    // 直接注入XML里的sample.select属性
    @Value("${sample.select}")
    private String userSelectSql;

    public void fetchActiveUsers() {
        // 在这里直接使用userSelectSql执行查询即可
        System.out.println("执行SQL: " + userSelectSql);
    }
}

方式二:注入整个Properties对象

如果你需要读取多个SQL属性,也可以直接注入整个Properties对象:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Properties;

@Repository
public class UserDao {

    private final Properties sqlQueries;

    // 构造注入整个Properties对象
    @Autowired
    public UserDao(Properties sqlQueries) {
        this.sqlQueries = sqlQueries;
    }

    public void fetchActiveUsers() {
        String selectSql = sqlQueries.getProperty("sample.select");
        // 使用selectSql执行查询
    }
}

这样你就能轻松在DAO里获取到XML属性文件中的SQL啦!

内容的提问来源于stack exchange,提问作者Renaud is Not Bill Gates

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:44:28