news 2026/2/9 0:04:16

Spring配置文件中加载properties文件

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring配置文件中加载properties文件

目录

  • 一、基于XML
    • 1.配置文件初始格式
    • 2.开启context命名空间
    • 3.使用context空间加载properties文件
    • 4.使用${}读取properties文件
  • 二、基于注解
    • 1.@PropertySource
    • 2.使用${}读取properties文件

一、基于XML

加载properties文件是为了在Spring的Bean配置文件中使用properties中的属性

1.配置文件初始格式

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><beanid="UsersMapperImpl"class="org.example.dao.UsersMapperImpl"/></bean>

2.开启context命名空间

  • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"复制一份并修改为xmlns:context="http://www.w3.org/2001/XMLSchema-instance"
  • xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd复制一份并修改beancontext
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><beanid="UsersMapperImpl"class="org.example.dao.UsersMapperImpl"/></bean>

3.使用context空间加载properties文件

使用同一个context空间可以一次性读取多个properties文件,但是推荐使用*进行后缀匹配加载所有properties文件,且添加classpath:确保读取的是类路径下的properties文件,防止读取到第三方jar包中的properties文件。

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--读取jdbc.properties属性配置文件 system-properties-mode:是否加载环境变量,如果不设置默认可以使用环境变量,且当properties文件中的属性与环境变量重名时会优先使用环境变量 --><context:property-placeholderlocation="classpath:*.properties"system-properties-mode="NEVER"/><beanid="UsersMapperImpl"class="org.example.dao.UsersMapperImpl"/></bean>

4.使用${}读取properties文件

<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><!--读取jdbc.properties属性配置文件--><context:property-placeholderlocation="jdbc.properties"></context:property-placeholder><!--创建数据源--><beanid="dataSource"class="com.alibaba.druid.pool.DruidDataSource"><propertyname="driverClassName"value="${jdbc.driverClassName}"></property><propertyname="url"value="${jdbc.url}"></property><propertyname="username"value="${jdbc.username}"></property><propertyname="password"value="${jdbc.password}"></property></bean></bean>

二、基于注解

1.@PropertySource

@Configuration相当于配置文件的初始格式,有关配置类的内容见Spring IoC注解。
@PropertySource用于开启context命名空间和加载properties文件。

@Configuration@ComponentScan("org.example")@PropertySource("jdbc.properties")publicclassMyConfig{}

2.使用${}读取properties文件

@ComponentpublicclassUsersServiceImplimplementsUsersService{@Autowired// 依赖注入UsersMapperusersMapper;@Value("${jdbc.name}")Stringname;}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/7 1:50:33

远视储备减少,近视风险增加!这样做守护孩子视力发育“储蓄罐”

在儿童视力发育进程中&#xff0c;远视储备是一个至关重要的概念&#xff0c;它就像孩子眼睛的“视力储蓄罐”&#xff0c;是眼睛应对日后用眼压力的天然缓冲。随着电子产品普及、孩子学业压力增大&#xff0c;很多家长发现孩子的远视储备正悄然减少&#xff0c;而这一信号的背…

作者头像 李华
网站建设 2026/2/5 7:15:30

Dify回调URL接收PyTorch模型异步预测结果

Dify回调URL接收PyTorch模型异步预测结果 在如今AI应用快速迭代的背景下&#xff0c;一个常见的工程挑战浮出水面&#xff1a;如何让训练好的深度学习模型真正“跑起来”&#xff0c;并稳定地服务于前端业务&#xff1f;尤其是当用户通过低代码平台发起请求时&#xff0c;若背后…

作者头像 李华
网站建设 2026/2/6 13:20:30

Conda环境导出为YAML文件,方便共享PyTorch配置

Conda环境导出为YAML文件&#xff0c;方便共享PyTorch配置 在深度学习项目协作中&#xff0c;你是否遇到过这样的场景&#xff1a;同事拉取你的代码后第一句话是“我的 torch.cuda.is_available() 返回 False”&#xff1f;或者你在复现一篇论文时&#xff0c;明明 pip install…

作者头像 李华
网站建设 2026/2/2 22:09:15

线性代数(十)四个基本子空间与矩阵空间

对于一个矩阵A&#xff08;m行n列&#xff09;&#xff0c;可以找到其对应的四个子空间&#xff0c;它们分别是&#xff1a; 1.列空间&#xff0c;C(A)&#xff1b;因为每个列向量是m维的&#xff0c;所以C&#xff08;A&#xff09;是的子空间2.零空间&#xff0c;N&#xff0…

作者头像 李华
网站建设 2026/2/2 12:37:48

Docker Volume持久化保存PyTorch训练数据

Docker Volume持久化保存PyTorch训练数据 在深度学习项目中&#xff0c;模型训练往往需要数小时甚至数天时间。你是否经历过这样的场景&#xff1a;训练到第80个epoch时&#xff0c;容器意外退出&#xff0c;所有中间结果瞬间丢失&#xff1f;或者团队成员因为环境差异导致“在…

作者头像 李华