6.5.1. 载入*.properties文件

         @RequestMapping("/config")@ResponseBodypublic void config() {try {Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource("/config.properties"));for(String key : properties.stringPropertyNames()) {String value = properties.getProperty(key);System.out.println(key + " => " + value);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}  

6.5.2. @Value 注解

application.properties

server.name=Linux
server.host=192.168.0.1,172.16.0.1
         @Value("${server.name}")private String name;@Value("${server.name:Windows}") 如果application.properties没有配置server.name那么默认值将是 Windowsprivate String name;@Value("${app.name:@null}") // app.name = nullprivate String name;@Value("#{'${server.host}'.split(',')}") private List<String> host;

6.5.3. @PropertySource 注解

@PropertySource("classpath:/config.properties}") 忽略FileNotFoundException,当配置文件不存在系统抛出FileNotFoundException并终止程序运行,ignoreResourceNotFound=true 会跳过使程序能够正常运行
@PropertySource(value="classpath:config.properties", ignoreResourceNotFound=true)      

载入多个配置文件

@PropertySources({  @PropertySource("classpath:config.properties"),  @PropertySource("classpath:db.properties")
})

test.properties

name=Neo
age=30
package cn.netkiller.web;import java.util.Date;import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
@PropertySource("classpath:test.properties")
public class TestController {@AutowiredEnvironment environment;@Value("${age}")private String age;public TestController() {// TODO Auto-generated constructor stub}// 环境变量方式@RequestMapping("/test/env")@ResponseBodypublic String env() {String message = environment.getProperty("name");return message;}@RequestMapping("/test/age")@ResponseBodypublic String age() {String message = age;return message;}}

原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

6.5. Properties相关推荐

  1. More than one file was found with OS independent path 'META-INF/rxjava.properties

    在app下的build.gradle里面的defaultConfig里面添加:  defaultConfig {        packagingOptions {             exclu ...

  2. maven项目密码md5加密_加密Spring Boot中的application.properties

    1 概述 什么?都2020年了还在Spring Boot的配置文件中写明文密码? (虽然是小项目,明文也没人看.) (明文简单快捷方便啊!!! ) (你看直接用户名root密码123456多么简单!! ...

  3. ItemAdding实现数据验证--中文字段,properties.AfterProperties值为null的问题

    最近写事件接收器,发现中文字段如果直接用properties.AfterProperties["申请人"]这样获取的值为null,无法得到值.后拉忽然发现用英文字段可以得到值.难道 ...

  4. java error could_Java.lang.Error: Properties init: Could not determine current working directory.

    用shell脚本编译项目,重新发布后,启动tomcat出现错误: Error occurred during initialization of VM java.lang.Error: Propert ...

  5. Java学习总结:57(Properties子类)

    Properties子类 Properties类本身是Hashtable的子类,但是由于Properties类都使用String数据类型进行操作,所以在使用Properties类时主要使用本类所定义的 ...

  6. java配置文件实现方式_java相关:详解Spring加载Properties配置文件的四种方式

    java相关:详解Spring加载Properties配置文件的四种方式 发布于 2020-4-29| 复制链接 摘记: 一.通过 context:property-placeholder 标签实现配 ...

  7. C3P0_and_pro.properties配置文档代码

    C3P0-config.xml配置文件 <c3p0-config> <!-- 默认配置,如果没有指定则使用这个配置 --> <default-config>< ...

  8. Properties持久的属性集

    Properties 属性集合继承了Hashtable 属性包括属性名和属性值(键值对key==value) 作用 可以存储多个键值,与map相似 可以把键值对存储到文件中 可以把文件中的键值对读取到 ...

  9. 使用Properties连接数据库

    使用Properties连接数据库 要注意的是: 1.通过配置文件来连接数据库时,连接信息要以 mysql.XXX开头,否则会提示异常. java.sql.SQLException: Access d ...

  10. Properties类读写配置文件

    Properties类读写简单配置文件相当的方便,以前竟然没注意到这个类. FileInputStream fs = new FileInputStream("config.ini" ...

最新文章

  1. linux的veth导致网络不通,linux的veth对网桥通信实验
  2. 从RNA-seq结果到差异表达
  3. Elasticsearch之分布式介绍
  4. modelsim仿真正确FPGA运行不正确的可能原因 - cm4写寄存器错
  5. PHP封装对象名字的思路
  6. 开始的一些知识和概念
  7. java 中的 Scanner
  8. java基础---File类
  9. javascript常用判断写法
  10. pythondev更新到3_python版本升级到3.7
  11. iOS精品源码,GHConsole图片浏览器圆形进度条音视频传输连击礼物弹出动画
  12. GitHub提交代码后不显示用户名只显示邮箱
  13. JQ6500语音模块
  14. 网页复制文字要收费怎么复制
  15. 左霆:无处不在的订阅经济
  16. 【IoT】产品设计:结构设计之什么是堆叠设计(一)
  17. 墨刀的html压缩包是什么,墨刀那些事
  18. 大数(10^9)求欧拉数
  19. his系统服务器选择,我院完成HIS服务器及数据库的升级改造
  20. 学习ZYNQ之FPGA2(开发板资源初探)

热门文章

  1. 面向接口编程详解---编程实例
  2. 中建股份400亿大型IPO获“特批”
  3. 【博客管理】短期长期计划【置顶】
  4. 【matlab】从图片中截取矩形区域(手工选取/标记在原图上/截取矩形区域并保存)
  5. 【Git/Github学习笔记】Git课程简介
  6. 控件时出现“未能实例化控件******,VS2008 中无法使用ACTIVEX控件的解决
  7. CMake Error at CMakeLists.txt:52 (PROJECT): No CMAKE_CXX_COMPILER could be found.
  8. 图像处理基础(8):图像的灰度直方图、直方图均衡化、直方图规定化(匹配)
  9. Py6S模块安装过程(Win10环境,64位)
  10. 利用composer搭建PHP框架(二.控制器)