SpringBoot学习:读取yml和properties文件的内容
一、在SpringBoot实现属性注入:
1)、添加pom依赖jar包;
<!-- 支持 @ConfigurationProperties 注解 -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot.version}</version>
</dependency>
2)、在yml配置文件中:
#pojo属性注入
Mybar: #pojo中的prefix值
name: 张三
arrs: 赵,钱,孙,李
nameList:
- name: 刘
value: 刘备
- name: 张
value: 张飞
BarNameList:
- 早退次数
- 迟到次数
- 旷工天数
map:
key1: 曹操
key2: 曹丕
key3: 曹植
3)、pojo通过set、get方法获取呀,yml中的值
package cn.com.venus.oa.pojo; import java.util.List;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /**
* 加载yaml配置文件的方法
* spring-boot更新到1.5.2版本后locations属性无法使用
* @PropertySource注解只可以加载proprties文件,无法加载yaml文件
* 故现在把数据放到application.yml文件中,spring-boot启动时会加载
*/
@Component
@ConfigurationProperties(prefix="Mybar")
public class Bar {
private String name; private String[] arrs; private List<Map<String,String>> nameList; private List<String> BarNameList; private Map<String,String> map; public String getName() {
return name;
} //String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要
public void setName(String name) {
this.name = name;
} public String[] getArrs() {
return arrs;
} public void setArrs(String[] arrs) {
this.arrs = arrs;
} public Map<String, String> getMap() {
return map;
} public void setMap(Map<String, String> map) {
this.map = map;
} public List<Map<String, String>> getNameList() {
return nameList;
} public void setNameList(List<Map<String, String>> nameList) {
this.nameList = nameList;
} public List<String> getBarNameList() {
return BarNameList;
} public void setBarNameList(List<String> barNameList) {
BarNameList = barNameList;
} }
4)、最终在Controller中执行自动注入就可以完成yml配置属性值:
@Autowired
private Bar bar;
二、properties配置文件:
使用@PropertySource注解加载配置文件,该注解无法加载yml配置文件。使用@Value注解获得文件中的参数值
package com.sun.configuration; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; /**
* 加载properties配置文件,在方法中可以获取
* abc.properties文件不存在,验证ignoreResourceNotFound属性
* 加上encoding = "utf-8"属性防止中文乱码,不能为大写的"UTF-8"
* Created by sun on 2017-3-30.
*/
@Configuration
@PropertySource(value = {"classpath:/config/propConfigs.properties","classpath:/config/abc.properties"},
ignoreResourceNotFound = true,encoding = "utf-8")
public class PropConfig { // PropertySourcesPlaceholderConfigurer这个bean,
// 这个bean主要用于解决@value中使用的${…}占位符。
// 假如你不使用${…}占位符的话,可以不使用这个bean。
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
获取properties文件参数值有两种方法,一种获得Environment 的对象,第二种就是@Value注解
@Autowired
private Environment env;
@Value("${age}")
String name; @RequestMapping("/")
@ResponseBody
String home(HttpServletRequest req) throws JsonProcessingException, UnsupportedEncodingException {
logger.info("测试通过!!!");
ObjectMapper objectMapper = new ObjectMapper();
//测试加载yml文件
System.out.println("simpleProp: " + config.getSimpleProp());
System.out.println("arrayProps: " + objectMapper.writeValueAsString(config.getArrayProps()));
System.out.println("listProp1: " + objectMapper.writeValueAsString(config.getListProp1()));
System.out.println("listProp2: " + objectMapper.writeValueAsString(config.getListProp2()));
System.out.println("mapProps: " + objectMapper.writeValueAsString(config.getMapProps())); //测试加载properties文件
System.out.println(env.getProperty("name"));//孙凯
System.out.println(env.getProperty("abc"));//null
System.out.println(name);//26 return "Hello World!";
}
SpringBoot学习:读取yml和properties文件的内容的更多相关文章
- eclipse中的.yml和.properties文件没有绿色叶子图标
0.首先确认正确安装了STS插件 要在eclipse使用spring boot创建项目,必须先安装STS(Spring Tool Suite (STS) for Eclipse),如果网速给力的话可以 ...
- java如何读取和遍历properties文件
在java项目开发过程中,使用properties文件作为配置基本上是必不可少的,很多如系统配置信息,文件上传配置信息等等都是以这种方式进行保存.同时学会操作properties文件也是java基础. ...
- 关于java读取和写入properties配置文件的内容
一般通过使用流的方式进行读取 代码示例如下: package com.zznode.transmit.util; import java.io.FileInputStream; import java ...
- Java 程序 关于Properties 类使用Store方法时不能会覆盖以前Properties 文件的内容
F:\\Demo.properties 文件内容: #\u65B0\u589E\u4FE1\u606F#Wed Sep 14 11:16:24 CST 2016province=广东tt=近蛋city ...
- 获取properties文件的内容
获取properties文件的内容 public void test() throws Exception{ String resource = "application.propertie ...
- SpringBoot学习:获取yml和properties配置文件的内容
项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @Configu ...
- SpringBoot学习:获取yml和properties配置文件的内容(转)
项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @Configu ...
- SpringBoot学习:获取yml和properties配置文件的内容(转)
项目下载地址:http://download.csdn.net/detail/aqsunkai/9805821 (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @Configu ...
- 解决IDEA springBoot读取*.properties文件中文内容乱码的问题
1. 配置 properties 文件 2. 读取 sex 属性输出到页面, 中文乱码 3. file --> settings 4. Editor --> File Encodings ...
随机推荐
- bzoj 2457 [BeiJing2011]双端队列 模拟+贪心
[BeiJing2011]双端队列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 457 Solved: 203[Submit][Status][D ...
- Qt -------- 多线程编程
一.继承QThread(不推荐) 定义一个类,继承QThread,重写run(),当调用方法start(),启动一个线程,run()函数运行结束,线程结束. 二.继承QRunnable Qrunnab ...
- Hadoop window win10 基础环境搭建(2.8.1)
下面运行步骤除了配置文件有部分改动,其他都是参照hadoop下载解压的share/doc/index.html. hadoop下载:http://apache.opencas.org/hadoop/c ...
- Java反射中method.isBridge() 桥接方法
桥接方法是 JDK 1.5 引入泛型后,为了使Java的泛型方法生成的字节码和 1.5 版本前的字节码相兼容,由编译器自动生成的方法.我们可以通过Method.isBridge()方法来判断一个方法是 ...
- Linux局域网内文件传送
先安装ssh服务 sudo apt-get install ssh 普通传输文件,可以使用scp命令 1.将本地文件复制到目标机器: scp 文件名 用户名@目标机器IP:目标机器路径 回车后输入密 ...
- JAVA JDBC(存储过程和事务管理)
1.什么是存储过程 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程 ...
- JS之document例题讲解2
例题三.图片轮播 <body> <div style="width:1000px; height:250px; margin-top:30px"> < ...
- gridveiw的使用
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- Android中TextView设置字体
最近项目中出现把字体设置成宋体,微软雅黑,黑体,楷体等的需求; 度娘发现Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace",除此之外还可以使 ...
- promise 如何知道所有的回调都执行完了?
var fs = require('fs'); /** * @return {object} Promise */ function doThing(fileName) { // ... // con ...