一、在SpringBoot实现属性注入:

  1)、添加pom依赖jar包;

1 <!-- 支持 @ConfigurationProperties 注解 -->
2 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor -->
3 <dependency>
4 <groupId>org.springframework.boot</groupId>
5 <artifactId>spring-boot-configuration-processor</artifactId>
6 <version>${spring-boot.version}</version>
7 </dependency>

  2)、在yml配置文件中:

 1 #pojo属性注入
2 Mybar: #pojo中的prefix值
3 name: 张三
4 arrs: 赵,钱,孙,李
5 nameList:
6 - name: 刘
7 value: 刘备
8 - name: 张
9 value: 张飞
10 BarNameList:
11 - 早退次数
12 - 迟到次数
13 - 旷工天数
14 map:
15 key1: 曹操
16 key2: 曹丕
17 key3: 曹植

  3)、pojo通过set、get方法获取呀,yml中的值

 1 package cn.com.venus.oa.pojo;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.beans.factory.annotation.Value;
8 import org.springframework.boot.context.properties.ConfigurationProperties;
9 import org.springframework.stereotype.Component;
10
11 /**
12 * 加载yaml配置文件的方法
13 * spring-boot更新到1.5.2版本后locations属性无法使用
14 * @PropertySource注解只可以加载proprties文件,无法加载yaml文件
15 * 故现在把数据放到application.yml文件中,spring-boot启动时会加载
16 */
17 @Component
18 @ConfigurationProperties(prefix="Mybar")
19 public class Bar {
20 private String name;
21
22 private String[] arrs;
23
24 private List<Map<String,String>> nameList;
25
26 private List<String> BarNameList;
27
28 private Map<String,String> map;
29
30 public String getName() {
31 return name;
32 }
33
34 //String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要
35 public void setName(String name) {
36 this.name = name;
37 }
38
39 public String[] getArrs() {
40 return arrs;
41 }
42
43 public void setArrs(String[] arrs) {
44 this.arrs = arrs;
45 }
46
47 public Map<String, String> getMap() {
48 return map;
49 }
50
51 public void setMap(Map<String, String> map) {
52 this.map = map;
53 }
54
55 public List<Map<String, String>> getNameList() {
56 return nameList;
57 }
58
59 public void setNameList(List<Map<String, String>> nameList) {
60 this.nameList = nameList;
61 }
62
63 public List<String> getBarNameList() {
64 return BarNameList;
65 }
66
67 public void setBarNameList(List<String> barNameList) {
68 BarNameList = barNameList;
69 }
70
71
72 }

  4)、最终在Controller中执行自动注入就可以完成yml配置属性值:

1 @Autowired
2 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注解

1 @Autowired
2 private Environment env;
3 @Value("${age}")
4 String name;
5
6
7 @RequestMapping("/")
8 @ResponseBody
9 String home(HttpServletRequest req) throws JsonProcessingException, UnsupportedEncodingException {
10 logger.info("测试通过!!!");
11 ObjectMapper objectMapper = new ObjectMapper();
12 //测试加载yml文件
13 System.out.println("simpleProp: " + config.getSimpleProp());
14 System.out.println("arrayProps: " + objectMapper.writeValueAsString(config.getArrayProps()));
15 System.out.println("listProp1: " + objectMapper.writeValueAsString(config.getListProp1()));
16 System.out.println("listProp2: " + objectMapper.writeValueAsString(config.getListProp2()));
17 System.out.println("mapProps: " + objectMapper.writeValueAsString(config.getMapProps()));
18
19 //测试加载properties文件
20 System.out.println(env.getProperty("name"));//孙凯
21 System.out.println(env.getProperty("abc"));//null
22 System.out.println(name);//26
23
24 return "Hello World!";
25 }

  

test read ymlfile

fruit: 
name: apple 
amount: 5

自定义的属性和值

myProps: 
simpleProp: simplePropValue 
arrayProps: 1,2,3,4,5 
listProp1: 
- name: abc 
value: abcValue 
- name: efg 
value: efgValue 
listProp2: 
- config2Value1 
- config2Vavlue2 
mapProps: 
key1: value1 
key2: value2

server: 
port: 8080 
address: localhost 
context-path: /springboot-jpa

1.@value注解


2.javaBean:


SpringBoot yml properties文件的更多相关文章

  1. SpringBoot读取配置文件(从classpath/file读取yml/properties文件)

    一.读取properties文件 使用配置项@PropertySource   二.读取yml文件 启动类添加下面代码: @Bean public static PropertySourcesPlac ...

  2. idea指定SpringBoot启动.properties文件

    比如我的项目下有2个.properties文件,一个是application.properties,一个是application-local.properties,在本地的时候想指定用applicat ...

  3. 解决IDEA springBoot读取*.properties文件中文内容乱码的问题

    1. 配置 properties 文件 2. 读取 sex 属性输出到页面, 中文乱码 3. file --> settings 4. Editor --> File Encodings ...

  4. 【问题篇四】SpringBoot的properties文件不能自动提示解决方法(1)

    一.Eclipse 解决方法:Eclipse中安装Spring Tools Suite(STS). 这里采用离线安装的方式. 1. 官网:https://spring.io/tools3/sts/al ...

  5. springboot获取properties文件的配置内容(转载)

    1.使用@Value注解读取读取properties配置文件时,默认读取的是application.properties. application.properties: demo.name=Name ...

  6. SpringBoot 获得 properties 文件中数据方式

    参考:https://blog.csdn.net/qq_37171353/article/details/78005845

  7. 关于properties文件的读取(Java/spring/springmvc/springboot)

    一.Java读取properties文件 1.基于ClassLoder读取配置文件 注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便. Properties prope ...

  8. SpringBoot @Value读取properties文件的属性

    SpringBoot在application.properties文件中,可以自定义属性. 在properties文件中如下示: #自定义属性 mail.fromMail.addr=lgr@163.c ...

  9. springboot-14-自定义properties文件值注入javaBean中

    被这个问题困扰了好几天.... 在spring中, 从资源文件向bean中注入值非常简单, 只需要properties文件被spring加载, 然后在被spring管理的类写响应的属性, 然后 @Va ...

随机推荐

  1. js jquery 设置cookie

    转自http://yaoqianglilan.blog.163.com/blog/static/70978316201091810435251/ 本人亲测setcookie() getcookie() ...

  2. BASIC-27_蓝桥杯_2n皇后问题

    题目: 问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一行.同一列或同一条对角线上,任意的两个白皇后都不在同一行.同一 ...

  3. Rabbit简单测试实例

    Rabbit简单测试实例 安装环境: Yum -y install python-pip Pip install pika 生产者 1 2 3 4 5 6 7 8 9 10 11 import pik ...

  4. RDD之五:Key-Value型Transformation算子

    Transformation处理的数据为Key-Value形式的算子大致可以分为:输入分区与输出分区一对一.聚集.连接操作. 输入分区与输出分区一对一 mapValues mapValues:针对(K ...

  5. 杂项-性能测试工具:LoadRunner

    ylbtech-杂项-性能测试工具:LoadRunner LoadRunner,是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadR ...

  6. shell 6基本运算符

    shell支持多种运算符: * 算数运算符 * 关系运算符 * 布尔运算符 * 字符串运算符 * 文件测试运算符 算数运算符 + 加 `expr $a + $b` 结果为 30 - 减 `expr $ ...

  7. mysql存储过程的参数名不要跟字段名一样 (血淋淋的代价)

    如题,将会导致的结果就是参数的值将不会是你传入的值,而是变成每条记录的那个字段的值. 这样的后果,是灰常严重的.比如执行删除操作,它能把整个表的记录全删了. 这个是我的血淋淋的代价啊. 死坑如下,勿跳 ...

  8. python 模块被引用多次但是里面的全局表达式总共只会执行一次

    python 模块被引用多次但是里面的全局表达式总共只会执行一次

  9. blktrace分析IO

    http://bean-li.github.io/blktrace-to-report/ 前言 上篇博客介绍了iostat的一些输出,这篇介绍blktrace这个神器.上一节介绍iostat的时候,我 ...

  10. OpenStack之日志

    OpenStack日志 日志对于一个稳定的系统来说相当重要,对于OpenStack这样一个大型的系统,日志当然也是必不可少,理解Openstack系统的日志对于保证OpenStack环境稳定非常重要. ...