前言

算是对《SpringBoot初学(2) - properties配置和读取》的总结吧。

概念性总结

一、Spring Boot允许外化(externalize)你的配置。可以使用properties文件,YAML文件,环境变量和命令行参数来外化配置。

使用@Value注解,可以直接将属性值注入到你的beans中,并通过Spring的Environment抽象或绑定到结构化对象来访问。

二、Spring Boot使用一个非常特别的PropertySource次序来允许对值进行合理的覆盖,需要以下面的次序考虑属性:

(尽量能理解了加载顺序在记忆,不强求记住。)
    1. 命令行参数
    2. 来自于java:comp/env的JNDI属性
    3. Java系统属性(System.getProperties())
    4. 操作系统环境变量
    5. 只有在random.*里包含的属性会产生一个RandomValuePropertySource
    6. 在打包的jar外的应用程序配置文件(application.properties,包含YAML和profile变量)
    7. 在打包的jar内的应用程序配置文件(application.properties,包含YAML和profile变量)
    8. 在@Configuration类上的@PropertySource注解
    9. 默认属性(使用SpringApplication.setDefaultProperties指定)

三、YAML相对properties的缺点

YAML文件不能通过@PropertySource注解加载。所以,在这种情况下,如果需要使用@PropertySource注解的方式加载值,那就要使用properties文件。

四、Spring boot类型安全的配置属性

在spring提供的读取properties中,运用注解@Value("${property}")注解注入配置属性有时可能比较笨重,特别是需要使用多个properties或你的数据本身有层次结构。

为了控制和校验你的应用配置,Spring Boot提供一个允许强类型beans的替代方法来使用properties。

当@EnableConfigurationProperties注解应用到你的@Configuration时,任何被@ConfigurationProperties注解的beans将自动被Environment属性配置。

这种风格的配置特别适合与SpringApplication的外部YAML配置进行配合使用。

五、Spring boot的松散绑定

(摘自:spring boot参考指南,101/420,23.7.2)

(暂时也没理解ConversionService到底要怎么写。)

一、java基本类型的properties读取

java的8种基本类型:逻辑型boolean、文本型char、整数型(byte、short、int、long)、浮点型(float、double)。对应的封装类型如Integer、Boolean等是一样的。

特殊类型String。

## BaseProperty.properties
byte_=1
short_=2
int_=23
long_=1234
float_=123.456
double_=12345.6789
char_=2
boolean_=true
## 中文乱码问题,不同的IDE问题处理不一样。
string_=str中文
## 日期不知如何直接注入成Date类型
date_=2017-02-13
package com.vergilyn.demo.springboot.properties.bean;

import javax.validation.constraints.NotNull;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Configuration
@ConfigurationProperties()
@PropertySource("classpath:config/properties/BaseProperty.properties")
@Component
public class BasePropertyBean {
private byte byte_ ;
private short short_ ;
private int int_ ;
private long long_ ;
private float float_ ;
private double double_ ;
private char char_ ;
private boolean boolean_ ;
@NotNull //加JSR-303 javax.validation约束注解
private String string_;
/* 不知道怎么直接注入Date类型*/
// private Date date_; // 省略set/get }

二、复杂类型

如Map,List,数组等。

## ComplexProperty.properties
## map
vergilyn.map[blog]=http://www.cnblogs.com/VergiLyn/
vergilyn.map[name]=VergiLyn
vergilyn.map[remark]=备注,中文23333 ## list
vergilyn.list[0]=Carpenters
vergilyn.list[1]=Celine Dion
vergilyn.list[2]=Bon Jovi
vergilyn.list[3]=Taylor Swift ## array
vergilyn.array[0]=I Don't Wanna Live Forever
vergilyn.array[1]=Safe And Sound
vergilyn.array[2]=22
vergilyn.array[4]=Unpredictable Story
vergilyn.array[8]=No One vergilyn.str=string
vergilyn.num=124
vergilyn.date=2017-01-14 23:55:19
vergilyn.isSuccess=false
package com.vergilyn.demo.springboot.properties.bean;

import java.util.List;
import java.util.Map; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Configuration
@ConfigurationProperties(prefix = "vergilyn")
@PropertySource("classpath:config/properties/ComplexProperty.properties")
@Component
public class ComplexPropertyBean {
private Map<String, Object> map;
private List<String> list;
private String[] array; //省略 set/get
}

三、随机数

如果要知道详细的,可以自行baidu、google。

## RandomPropertyBean.properties
## 随机值注入
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}
package com.vergilyn.demo.springboot.properties.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Configuration
@ConfigurationProperties()
@PropertySource("classpath:config/properties/RandomProperty.properties")
@Component
public class RandomPropertyBean { @Value("${my.secret}")
private String secret;
@Value("${my.number}")
private int number;
@Value("${my.bignumber}")
private long bignumber;
@Value("${my.number.less.than.ten}")
private int intten;
@Value("${my.number.in.range}")
private int range; // 省略 set/get
}

(注意可能需要在application.properties中配置@Value常量。详见github源码。)

package com.vergilyn.demo.springboot.properties;

import com.vergilyn.demo.springboot.properties.bean.BasePropertyBean;
import com.vergilyn.demo.springboot.properties.bean.ComplexPropertyBean;
import com.vergilyn.demo.springboot.properties.bean.RandomPropertyBean; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @Configuration
@ComponentScan
@EnableAutoConfiguration
@PropertySource(value={"classpath:config/application.properties"}
, ignoreResourceNotFound = true)
@Controller
public class PropertyApplication { @Autowired
BasePropertyBean base;
@Autowired
ComplexPropertyBean complex;
@Autowired
RandomPropertyBean random;
// @Autowired
// RelaxedBindPropertyBean relaxed; @Value("${CONSTANT_PASSWORD}")
private String password; public static void main(String[] args) {
SpringApplication.run(PropertyApplication.class, args);
} @RequestMapping("/property")
public String property(@RequestParam(value="name", required=false, defaultValue="${CONSTANT_USER}") String name
, Model model) {
model.addAttribute("name", name);
model.addAttribute("password", password);
System.out.println(base);
System.out.println(complex);
System.out.println(random);
// System.out.println(relaxed);
return "custom";
}
}

PropertyApplication.java

github :

https://github.com/vergilyn/SpringBootDemo

自己学习spring boot的项目,并不是像官方samples一样是一个功能是一个项目,我是把所有都放在一个项目。

所以注意看application-{profile}.properties和application.properties的配置。

因为是一个项目,所以pom引入了很多jar。所以,可能有些properties配置是必须的。

【spring boot】SpringBoot初学(2.1) - properties读取明细的更多相关文章

  1. Spring Boot]SpringBoot四大神器之Actuator

    论文转载自博客: https://blog.csdn.net/Dreamhai/article/details/81077903 https://bigjar.github.io/2018/08/19 ...

  2. 【spring boot】使用@Value映射properties文件属性

    描述 使用@Value映射properties文件属性到Java字段 重点 使用@PropertySource 注解指定*.properties文件位置: 使用@Value进行注入: my.prope ...

  3. 在spring boot中使用自定义的properties

    1 在application.properties中添加 android.name=Tim android.password=123456 新建一个保存该Setting的配置类, @Configura ...

  4. Spring Boot属性配置文件:application.properties 详解

    学习资料 网址 官方说明文档 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-pro ...

  5. spring boot 在框架中注入properties文件里的值(Spring三)

    前一篇博客实现了打开第一个页面 链接:https://blog.csdn.net/qq_38175040/article/details/105709758 本篇博客实现在框架中注入propertie ...

  6. Spring Boot中注入配置文件application.properties中的list 对象参数

    例如要注入下列参数: dyn.spring.datasources[0].name=branchtadyn.spring.datasources[0].driverClassName=oracle.j ...

  7. Spring Boot 配置文件详解:Properties和YAML

    一.配置文件的生效顺序,会对值进行覆盖: 1. @TestPropertySource 注解 2. 命令行参数 3. Java系统属性(System.getProperties()) 4. 操作系统环 ...

  8. [Spring boot] Read values from external properties file

    Let's say we have a extral app.proporites file which contains some extra configuration: // resources ...

  9. Spring boot 梳理 - 全局配置文件application.properties或者是application.yml,在resources目录下或者类路径下的/config下,一般我们放到resources下。

    全局配置文件application.properties或者是application.yml,在resources目录下或者类路径下的/config下,一般我们放到resources下.

随机推荐

  1. 使用MS Devops 来部署CRM Solution

    在D365 CE开发当中,有一个非常痛苦的问题就是开发,测试环境中的export import solution 部署问题. Devops中能很好的解决这个问题. 工作原理: 在Azure Devop ...

  2. 深入理解JVM(二)--垃圾收集算法

    一. 概述 说起垃圾收集(Garbage Collection, GC), 大部分人都把这项技术当做Java语言的伴随生产物. 事实上, GC的历史远远比Java久远, 1960年 诞生于MIT的Li ...

  3. Cmake知识----编写CMakeLists.txt文件编译C/C++程序(转)

    1.CMake编译原理 CMake是一种跨平台编译工具,比make更为高级,使用起来要方便得多.CMake主要是编写CMakeLists.txt文件,然后用cmake命令将CMakeLists.txt ...

  4. VFP 的 SPT 起跳 -- 陈纯(BOE数据网络工作室)

    细节描述 Visual FoxPro 的 SPT 技术快速入门 说在前面熟悉 Fox 的朋友都知道,在 VFP 里我们可以使用远程视图 (Remote View) 和 SPT(SQL Pass Thr ...

  5. NPOI word文档表格在新的文档中多次使用

    最近有一个项目,涉及到文档操作,有一个固定的模版,模版中有文字和表格,表格会在新的文档中使用n多次 //获取模版中的表格FileStream stream = new FileStream(strPa ...

  6. 解决shiro自定义filter后,ajax登录无法登录,并且无法显示静态资源的问题

    这个问题困扰了我一天,看了下面两个文章,豁然开朗: https://www.cnblogs.com/gj1990/p/8057348.html https://412887952-qq-com.ite ...

  7. C++中的多态及虚函数大总结

    多态是C++中很关键的一部分,在面向对象程序设计中的作用尤为突出,其含义是具有多种形式或形态的情形,简单来说,多态:向不同对象发送同一个消息,不同的对象在接收时会产生不同的行为.即用一个函数名可以调用 ...

  8. 安全扫描工具Acunetix即AWVS_13.x系列破解版Linux & Windows

    本站所提供工具仅供技术学习交流.请勿用于非法行为.否则后果自负. Acunetix,自动化网络应用安全软件的先驱,已经宣布发布Acunetix第13版.新版本提供了一个改进的用户界面,并引入了创新,如 ...

  9. MySQL之ERROR 1558 (HY000): Column count of mysql.user is wrong.解决方案

    一.场景 我本想在MySQL5.7上执行下列语句创建一个新用户: CREATE USER "remote"@"%" IDENTIFIED BY "12 ...

  10. AndroidStudio中使用XML和Java代码混合控制UI界面实现QQ相册照片列表页面

    场景 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建Androi ...