SpringMVC加载配置Properties文件的几种方式

详解SpringMVC加载配置Properties文件的几种方式 主要介绍了详解SpringMVC加载配置Properties文件的几种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。 立即下载

 SpringMVC加载配置Properties文件的几种方式 

         最近开发的项目使用了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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="mytest" />
<!-- 强制SPRING使用CGLIB动态代理,不使用JDK代理。 -->
<aop:config proxy-target-class="true" />
<!-- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
<property name="locations"> <list> <value>classpath:jdbc.properties</value> 
</list> </property> </bean> -->
    <!--  <context:property-placeholder location="classpath:jdbc.properties" /> -->
<util:properties id="settings" location="classpath:config.properties"/>
</beans>


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、在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.5、在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;  
     }   
}  
返回如下结果:
{   
    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;  
      }   
}  
返回如下结果:
{  
   "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  
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、测试
返回如下结果:
{  
   "gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"  
 }  
3.4、实例如下:
1)package com.websystique.spring;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import com.websystique.spring.configuration.AppConfig;
import com.websystique.spring.service.FileService;
public class AppMain {
public static void main(String args[]){
AbstractApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
FileService service = (FileService) context.getBean("fileService");
service.readValues();
context.close();

}


2)package com.websystique.spring.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan(basePackages = "com.websystique.spring")
@PropertySource(value = { "classpath:application.properties" })

public class AppConfig {
/*
* PropertySourcesPlaceHolderConfigurer Bean only required for @Value("{}") annotations.
* Remove this bean if you are not using @Value annotations for injecting properties.
*/
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
}
3)package com.websystique.spring.service;
public interface FileService {
void readValues();
}
4)package com.websystique.spring.service
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
@Service("fileService")
public class FileServiceImpl implements FileService {
@Value("${sourceLocation:c:/temp/input}")
private String source;
@Value("${destinationLocation:c:/temp/output}")
private String destination;
@Autowired
private Environment environment;
public void readValues() {
System.out.println("Getting property via Spring Environment :"
+ environment.getProperty("jdbc.driverClassName"));
System.out.println("Source Location : " + source);
System.out.println("Destination Location : " + destination);
}
}
Jupyter Notebook 命名出现“error”且运行后没反应 Jupyter Notebook 命名出现“error”且运行后没反应提前声明错误截图(方便大家看看是不是自己的错误)解决方法 提前声明 我的问题是在看到答主 @开始King的回答后才解决的,因为答主大大的文章后面有些地方不是很详细,我在这里再在King大大的基础上完善一下子 错误截图(方便大家看看是不是自己的错误) 1.命名失败,上面出现一个“error”字样 2.运行失败,无论是点击上方的运行按钮还是按快捷键shift+enter 都只会向下再增加一个代码块,而不会运行程序。 (也不是很多人出现的[* 阅读详情

相关推荐

三菱JET伺服限位、原点信号通过PLC刷新设置方法

RWw及RWr软元件的初始设定值,可通过 [Pr. PA01.0 控制模式选择] 及 [Pr. PN22.0 默认映射模式选择] 的值进行切换。JET伺服参数用于CC-Link IE现场网络Basic。循环通信的请求报文与响应报文的收发数据被换读为伺服放大器的对象数据 (RWwn、RWrn)。接下来看映射字2D05具体的内容: 也就是2D05操作字的第九位、第十位、第十一位。响应报文的设定值可进行变更。变更初始设定值时,应变更PDO映射。不同的模式,对应不同的刷新映射内容。GXWORKS3程序。

qq_51166033的博客 8066

Spring MVC如何读取配置文件

这样就可以通过Spring MVC读取配置文件了。现在,可以在Java类中使用属性变量。例如,如果配置文件中有一个属性名为。

程序员写的技术 FAQ 和杂文 1924

OpenCV视频防抖算法实现与优化指南

本文详细解析了如何使用OpenCV实现视频防抖算法。通过特征点检测与光流跟踪估算帧间运动,利用滑动平均平滑运动轨迹,并应用仿射变换重塑视频帧。文章提供了完整的代码实现、参数调优指南及黑边处理等优化技巧,帮助开发者高效处理手持拍摄等场景下的视频抖动问题。

weixin_29062823的博客 345

五种方式让你在java中读取properties文件内容不再是难题

一、背景   最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题。就借此机会把Spring+SpringMVC+Mybatis整合开发的项目中通过java程序读取properties文件内容的方式进行了梳理和分析,现和大家共享。 二、项目环境介绍     Spring 4.2.6.RELE...

Realize My Dream 7221

Java8对接三方流式接口,并实时输出(GPT)

【代码】Java8对接三方流式接口,并实时输出(GPT)

m0_58617940的博客 1730

STM32U575 ADC+DMA+UART数据采集实战:CubeMX配置避坑与代码实现

在嵌入式开发中,ADC采集与DMA传输是提升系统实时性的关键技术。通过DMA将ADC数据直接搬运到内存,可大幅降低CPU占用,实现高效连续采样。结合UART的DMA发送,能进一步解放CPU,适用于工业采集、信号监测等场景。本文以STM32U575为例,详解CubeMX中ADC、DMA、UART的配置要点,包括时钟树、DMAMUX映射、循环模式等,并给出代码实现与常见问题排查,帮助开发者快速构建稳定的数据采集链路。

weixin_29194255的博客 176

SpringMVC读取配置文件

1.注解读取@PropertySource:源文件 global.properties: Controller层: 其他类引入,可增加注解,如:@Configuration@Component

木辰風的博客 1861

Codex辅助小说创作:零成本AI写作指南与实战技巧

自然语言处理技术通过深度学习模型理解语义上下文,其核心原理是基于大规模文本训练的语言模型预测能力。在创意写作领域,这项技术的价值在于突破传统写作工具的功能局限,实现从内容生成到风格模仿的智能化辅助。特别是提示词工程的应用,让创作者能够精准控制AI输出的内容和风格。在实际应用场景中,AI写作工具显著降低了小说创作门槛,适用于网络小说、奇幻科幻等多种文学类型。本文以OpenAI Codex为例,详细解析如何利用AI技术完成从故事大纲设计、章节生成到平台发布的全流程创作,其中提示词优化和内容质量把控是提升生成效果

weixin_32678995的博客 593

mvcday01

Spirng Mvc 三层架构介绍 咱们开发服务器端程序,一般都基于两种形式,一种C/S架构程序,一种B/S架构程序 使用Java语言基本上都是开发B/S架构的程序,B/S架构又分成了三层架构 三层架构 表现层:WEB层,用来和客户端进行数据交互的。 业务层:处理公司具体的业务逻辑的。 持久层:用来操作数据库的。 MVC设计模式介绍 MVC全名是Model View Controller 模型视图控制器,每个部分各司其职。 Model:数据模型,JavaBean的类,用来进行数据封装。 View:指J

Lakn_an的博客 448

win x64下安装oracle 12c出现[INS-30131]报错的解决方案

问题:  win x64下安装oracle 12c出现[INS-30131]报错的解决方案 安装oracle11g碰到“无法访问临时位置”的问题,详细信息如下: INS-30131] 执行安装程序验证所需的初始设置失败。 原因 - 无法访问临时位置。   操作 - 请确保当前用户具有访问临时位置所需的权限。   附加信息:  - 所有节点上的框架设置检查都失败  

一条道走到黑的GISer 1351

【YOLOv26 源码深度解析】第三章:YOLOv26特征融合(Neck)模块

路径聚合网络作为YOLOv26 Neck部分的核心组件,在继承特征金字塔网络(FPN)自顶向下语义传播能力的基础上,引入了自底向上的路径增强机制,构建起双向跨层特征融合体系。该架构通过建立多尺度特征间的双向信息流,有效缓解了深层网络中细节信息逐层衰减的问题,使得模型在保持高层语义理解能力的同时,能够精确定位不同尺度的目标实例。跨层深度监督的另一优势在于增强特征的尺度适应性。对于来自上层路径的特征与横向连接的特征,融合过程首先对各路特征进行加权,随后进行归一化处理,最终通过加法或拼接操作生成融合特征。

u012133341的博客 524

java 连接springmvc配置文件_详解SpringMVC加载配置Properties文件的几种方式

最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大,网上介绍Contrhttp://www.cppcns.comoller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式1.通过context:property-placeholde实现配置文件加载1.1、在spring...

weixin_34433661的博客 335

YOLOv8改进 | 注意力机制 | 添加HAttention(HAT)超分辨率重建助力小目标检测 (全网首发,超分辨率检测)

本文给大家带来的改进机制是HAttention注意力机制,混合注意力变换器(HAT)的设计理念是通过融合通道注意力和自注意力机制来提升单图像超分辨率重建的性能。通道注意力关注于识别哪些通道更重要,而自注意力则关注于图像内部各个位置之间的关系。HAT利用这两种注意力机制,有效地整合了全局的像素信息,从而提供更为精确的结果(这个注意力机制挺复杂的光代码就700+行),但是效果挺好的也是10月份最新的成果非常适合添加到大家自己的论文中。⭐⭐⭐⭐⭐(最新的改进机制)

Snu77的博客 1万+

项目中加载服务器配置文件,SpringMVC加载配置Properties文件的几种方式

最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大,网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式1.通过context:property-placeholde实现配置文件加载1.1、在spring.xml中加入context相关引用[ht...

weixin_39593523的博客 586

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

java注解引用配置文件变量,详解SpringMVC加载配置Properties文件的几种方式

最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大,网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式1.通过context:property-placeholde实现配置文件加载1.1、在spring.xml中加入context相关引用?1....

weixin_28909779的博客 907

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

springmvc加载properties文件的几种方式

最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定、URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式 1.通过context:property-placeholde实现配置文件加载   1.1、在spring.xml中加入co

yinbucheng的博客 3280

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

上一篇: 怎样快捷利用Windows7截图工具
下一篇: ServletContextListener 解析用法
雨轩_wscrf
博客等级 码龄12年 41粉丝 165原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值