详解SpringMVC加载配置Properties文件的几种方式
发布时间 - 2026-01-10 23:16:57 点击率:次最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大,

网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式
1.通过context:property-placeholde实现配置文件加载
1.1、在spring.xml中加入context相关引用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
1.2、引入jdbc配置文件
<context:property-placeholder location="classpath:jdbc.properties"/>
1.3、jdbc.properties的配置如下
jdbc_driverClassName=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8 jdbc_username=root jdbc_password=123456
1.4、在spring-mybatis.xml中引用jdbc中的配置
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close" >
<property name="driverClassName">
<value>${jdbc_driverClassName}</value>
</property>
<property name="url">
<value>${jdbc_url}</value>
</property>
<property name="username">
<value>${jdbc_username}</value>
</property>
<property name="password">
<value>${jdbc_password}</value>
</property>
<!-- 连接池最大使用连接数 -->
<property name="maxActive">
<value>20</value>
</property>
<!-- 初始化连接大小 -->
<property name="initialSize">
<value>1</value>
</property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait">
<value>60000</value>
</property>
<!-- 连接池最大空闲 -->
<property name="maxIdle">
<value>20</value>
</property>
<!-- 连接池最小空闲 -->
<property name="minIdle">
<value>3</value>
</property>
<!-- 自动清除无用连接 -->
<property name="removeAbandoned">
<value>true</value>
</property>
<!-- 清除无用连接的等待时间 -->
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<!-- 连接属性 -->
<property name="connectionProperties">
<value>clientEncoding=UTF-8</value>
</property>
</bean>
1.5、在Java类中引用jdbc.properties中的配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JdbcConfig{
@Value("${jdbc_url}")
public String jdbcUrl; //这里变量不能定义成static
@Value("${jdbc_username}")
public String username;
@Value("${jdbc_password}")
public String password;
}
1.6、在controller中调用
@RequestMapping("/service/**")
@Controller
public class JdbcController{
@Autowired
private JdbcConfig Config; //引用统一的参数配置类
@Value("${jdbc_url}")
private String jdbcUrl; //直接在Controller引用
@RequestMapping(value={"/test"})
public ModelMap test(ModelMap modelMap) {
modelMap.put("jdbcUrl", Config.jdbcUrl);
return modelMap;
}
@RequestMapping(value={"/test2"})
public ModelMap test2(ModelMap modelMap) {
modelMap.put("jdbcUrl", this.jdbcUrl);
return modelMap;
}
}
1.7、测试
在ie中输入http://localhost:8080/testWeb/service/test 或http://localhost:8080/testWeb/service/test2
返回如下结果:
{
jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8"
}
注:通过context:property-placeholde加载多个配置文件
只需在第1.2步中将多个配置文件以逗号分隔即可
复制代码 代码如下:
<context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/>
2、通过util:properties实现配置文件加载
2.1、在spring.xml中加入util相关引用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
2.2、 引入config配置文件
<util:properties id="settings" location="classpath:config.properties"/>
2.3、config.properties的配置如下
gnss.server.url=http://127.0.0.1:8080/gnss/services/data-world/rest
2.4、在java类中引用config中的配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Config {
@Value("#{settings['gnss.server.url']}")
public String GNSS_SERVER_URL;
}
2.5、在controller中调用
@RequestMapping("/service2/**")
@Controller
public class ConfigController{
@Autowired
private Config Config; //引用统一的参数配置类
@RequestMapping(value={"/test"})
public ModelMap test(ModelMap modelMap) {
modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL);
return modelMap;
}
}
2.6、测试
在ie中输入http://localhost:8080/testWeb/service2/test
返回如下结果:
{
"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
}
3.直接在Java类中通过注解实现配置文件加载
3.1、在java类中引入配置文件
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value="classpath:config.properties")
public class Config {
@Value("${gnss.server.url}")
public String GNSS_SERVER_URL;
@Value("${gnss.server.url}")
public String jdbcUrl;
}
3.2、在controller中调用
@RequestMapping("/service2/**")
@Controller
public class ConfigController{
@Autowired
private Config Config; //引用统一的参数配置类
@RequestMapping(value={"/test"})
public ModelMap test(ModelMap modelMap) {
modelMap.put("gnss.service.url", Config.GNSS_SERVER_URL);
return modelMap;
}
}
3.3、测试
在ie中输入http://localhost:8080/testWeb/service2/test
返回如下结果:
{
"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
}
最后附上spring.xml的完整配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 引入多配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/>
<!-- 通过util引入config配置文件 -->
<!-- <util:properties id="settings" location="classpath:config.properties" /> -->
<!-- 扫描文件(自动将servicec层注入) -->
<context:component-scan base-package="修改成你的Config类所在的package"/></beans>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# springmvc
# properties
# spring加载properties
# SpringMVC配置Properties
# 在IDEA中搭建最小可用SpringMVC项目(纯Java配置)
# 在Spring中基于Java类进行配置的完整步骤
# 使用纯java config来配置spring mvc方式
# 配置文件
# 加载
# 类中
# 多个
# 连接池
# 只需
# 几种
# 绑定
# 大家多多
# 连接数
# 写这篇
# 使用了
# 网上
# 博客
# Driver
# jdbc_url
# mysql
# jdbc_driverClassName
# true
# amp
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
米侠浏览器网页背景异常怎么办 米侠显示修复
儿童网站界面设计图片,中国少年儿童教育网站-怎么去注册?
Win11怎么关闭资讯和兴趣_Windows11任务栏设置隐藏小组件
linux写shell需要注意的问题(必看)
Laravel辅助函数有哪些_Laravel Helpers常用助手函数大全
网站建设整体流程解析,建站其实很容易!
laravel怎么为API路由添加签名中间件保护_laravel API路由签名中间件保护方法
Laravel如何安装Breeze扩展包_Laravel用户注册登录功能快速实现【流程】
Swift中循环语句中的转移语句 break 和 continue
怎么制作一个起泡网,水泡粪全漏粪育肥舍冬季氨气超过25ppm,可以有哪些措施降低舍内氨气水平?
手机怎么制作网站教程步骤,手机怎么做自己的网页链接?
韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐
微信小程序 require机制详解及实例代码
Laravel怎么实现支付功能_Laravel集成支付宝微信支付
详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
JS碰撞运动实现方法详解
如何快速生成凡客建站的专业级图册?
HTML 中动态设置元素 name 属性的正确语法详解
JavaScript Ajax实现异步通信
如何在景安云服务器上绑定域名并配置虚拟主机?
Claude怎样写结构化提示词_Claude结构化提示词写法【教程】
Laravel如何配置和使用缓存?(Redis代码示例)
微信小程序 input输入框控件详解及实例(多种示例)
laravel怎么为应用开启和关闭维护模式_laravel应用维护模式开启与关闭方法
如何使用 jQuery 正确渲染 Instagram 风格的标签列表
详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)
如何在云主机上快速搭建多站点网站?
Swift中swift中的switch 语句
高防服务器租用首荐平台,企业级优惠套餐快速部署
bing浏览器学术搜索入口_bing学术文献检索地址
laravel怎么使用数据库工厂(Factory)生成带有关联模型的数据_laravel Factory生成关联数据方法
Laravel怎么进行数据库事务处理_Laravel DB Facade事务操作确保数据一致性
如何彻底删除建站之星生成的Banner?
Laravel Sail是什么_基于Docker的Laravel本地开发环境Sail入门
Laravel如何使用Scope本地作用域_Laravel模型常用查询逻辑封装技巧【手册】
使用C语言编写圣诞表白程序
JavaScript中如何操作剪贴板_ClipboardAPI怎么用
Laravel如何实现数据导出到CSV文件_Laravel原生流式输出大数据量CSV【方案】
javascript和jQuery中的AJAX技术详解【包含AJAX各种跨域技术】
Laravel表单请求验证类怎么用_Laravel Form Request分离验证逻辑教程
如何在宝塔面板中创建新站点?
千库网官网入口推荐 千库网设计创意平台入口
,交易猫的商品怎么发布到网站上去?
Win11搜索不到蓝牙耳机怎么办 Win11蓝牙驱动更新修复【详解】
Laravel如何实现用户角色和权限系统_Laravel角色权限管理机制
lovemo网页版地址 lovemo官网手机登录
制作企业网站建设方案,怎样建设一个公司网站?
北京网站制作公司哪家好一点,北京租房网站有哪些?
Win10如何卸载预装Edge扩展_Win10卸载Edge扩展教程【方法】
如何打造高效商业网站?建站目的决定转化率

