news 2026/3/5 2:57:50

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/26 21:28:27

Dockerfile定制你的PyTorch-CUDA镜像,灵活扩展功能

Dockerfile定制你的PyTorch-CUDA镜像&#xff0c;灵活扩展功能 在深度学习项目开发中&#xff0c;最让人头疼的往往不是模型调参&#xff0c;而是环境配置——“在我机器上能跑”成了团队协作中的经典噩梦。CUDA版本不匹配、cuDNN缺失、PyTorch编译出错……这些问题消耗了大量本…

作者头像 李华
网站建设 2026/2/25 1:12:46

SSH免密登录配置步骤,简化PyTorch容器访问

SSH免密登录配置实践&#xff1a;高效访问PyTorch-CUDA容器 在现代AI开发中&#xff0c;研究人员常常面对一个看似微小却频繁出现的痛点——每次连接远程训练环境都要输入密码。尤其是在需要反复调试、批量提交任务或集成自动化流水线时&#xff0c;这种重复操作不仅打断思路&a…

作者头像 李华
网站建设 2026/2/28 18:22:06

前端字节面试大纲

字节前端面试近期以基础深度工程化框架原理算法编程网络与浏览器为核心&#xff0c;一面重基础与手写&#xff0c;二面偏场景与方案设计&#xff0c;三面会结合字节业务&#xff08;如抖音、直播&#xff09;考察性能与架构能力。以下按模块整理高频题&#xff0c;附面试友好的…

作者头像 李华
网站建设 2026/3/1 5:10:24

PyTorch模型保存与加载最佳实践(支持GPU/CPU混合)

PyTorch模型保存与加载最佳实践&#xff08;支持GPU/CPU混合&#xff09; 在深度学习项目中&#xff0c;一个训练好的模型能否顺利从实验室走向生产环境&#xff0c;往往不取决于算法本身&#xff0c;而在于那些看似“基础”的工程细节——比如如何正确地保存和加载模型。尤其当…

作者头像 李华
网站建设 2026/3/3 14:59:26

Git标签管理PyTorch模型版本,便于回溯与发布

Git标签管理PyTorch模型版本&#xff0c;便于回溯与发布 在深度学习项目的日常开发中&#xff0c;你是否曾遇到这样的场景&#xff1a;几天前训练出一个准确率达到94.2%的模型&#xff0c;但如今却无法复现结果&#xff1f;或者团队成员跑出来的性能不一致&#xff0c;排查半天…

作者头像 李华
网站建设 2026/3/4 5:14:53

YOLOv11与RT-DETR对比实验:基于PyTorch-CUDA环境测评

YOLOv11与RT-DETR对比实验&#xff1a;基于PyTorch-CUDA环境测评 在智能监控、自动驾驶和工业质检等现实场景中&#xff0c;目标检测的性能直接决定了系统的响应速度与决策准确性。面对日益复杂的视觉任务&#xff0c;如何在有限算力下实现高精度、低延迟的检测&#xff0c;成为…

作者头像 李华