☰
加载properties文件
2026/10/14 9:22:30 网站建设 项目流程

准备

jdbc.properties

BookDao

BookDaoImpl

App

一、开启context命名空间

<?xml version="1.0"encoding="UTF-8"?><beans xmlns="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 http://www.springframework.org/schema/context/spring-context.xsd ">

二、使用context空间加载properties文件

三、使用属性占位符${}读取properties文件中的属性

执行

注意:
如果注入username会出问题


此时输出结果不是想要的

是因为被系统属性值覆盖了

解决:添加system-properties-mode="NEVER"

加载多个配置文件

<!--加载多个properties文件--><context:property-placeholder location="jdbc.properties,jdbc2.properties"system-properties-mode="NEVER"/><!--加载所有properties文件--><context:property-placeholder location="*.properties"system-properties-mode="NEVER"/><!--加载properties文件标准格式--><context:property-placeholder location="jdbc.properties,jdbc2.properties"system-properties-mode="NEVER"/><!--从类路径或jar包中搜索并加载properties文件--><context:property-placeholder location="jdbc.properties,jdbc2.properties"system-properties-mode="NEVER"/>

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询