2springboot配置文件解析

2.1application.properties配置文件

使用application.properties全局配置文件(位置为src/main/resources目录下或类路径的config下),提供自定义属性支持。

2.1.1自定义属性配置及使用

1)在application.properties文件中添加:

com.dudu.name="test"

com.dudu.want="祝大家鸡年大吉"

2)通过注解使用@Value(value=”${config.name}”)即可绑定

@RestController

public class UserController {

@Value("${com.dudu.name}")

private  String name;

@Value("${com.dudu.want}")

private  String want;

@RequestMapping("/")

public String hexo(){

return name+","+want;

}

}

2.1.2属性过多时绑定对象

1)创建一个ConfigBean.java类,使用注解@ConfigurationProperties(prefix=”com.dudu”)指明使用哪个

@ConfigurationProperties(prefix = "com.dudu")

public class ConfigBean {

private String name;

private String want;

// 省略getter和setter

}

2)还需配置spring boot入口类:

添加@EnableConfigurationProperties并指明加载哪个bean

@SpringBootApplication

@EnableConfigurationProperties({ConfigBean.class})

public class MyApplication {

public static void main(String[] args) {

SpringApplication.run(MyApplication.class, args);

}

}

3)最后在controller类中引入configbean使用:

@RestController

public class UserController {

@Autowired

ConfigBean configBean;

@RequestMapping("/")

public String hexo(){

return configBean.getName()+configBean.getWant();

}

}

2.1.3参数间引用

application.properties中的各个参数之间也可以直接引用来使用:

com.dudu.name="test"

com.dudu.want="祝大家鸡年大吉"

com.dudu.yearhope=${com.dudu.name}在此${com.dudu.want}

2.2自定义配置文件

自定义配置文件,避免全部放在application.properties中

1)      新建文件test.properties(路径如src/main/resources),添加配置

com.md.name="哟西~"

com.md.want="祝大家鸡年,大吉吧"

2)      新建bean类

@Configuration

@ConfigurationProperties(prefix = "com.md")

@PropertySource("classpath:test.properties")

public class ConfigTestBean {

private String name;

private String want;

// 省略getter和setter

}

1.5之前的配置可为:

@ConfigurationProperties(prefix = "config2",locations="classpath:test.properties")

2.3随机值配置

配置文件中${random} 可以用来生成各种不同类型的随机值,从而简化了代码生成的麻烦。如生成int 值、long 值或者 string 字符串

dudu.secret=${random.value}

dudu.number=${random.int}

dudu.bignumber=${random.long}

dudu.uuid=${random.uuid}

dudu.number.less.than.ten=${random.int(10)}

dudu.number.in.range=${random.int[1024,65536]}

2.4外部配置(命令行参数配置)

1)Spring boot是基于jar包运行的,可打成jar包并用命令行运行。

Java –jar xx.jar

或修改tomcat端口号:

Java –jar xx.jar –server.port=9090

其中—即对application.properties中属性进行赋值标识。相当于在配置文件中添加server.port=9090.

2)可通过SpringApplication.setAddCommandLineProperties(false)禁用命令行。

3)      配置文件优先级

application.properties和application.yml文件可以放在以下四个位置(按照优先级排序):

外置,在相对于应用程序运行目录的/congfig子目录里。

外置,在应用程序运行的目录里

内置,在config包内

内置,在Classpath根目录

3.1)src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性

3.2) 在相同优先级位置同时有application.properties和application.yml,那么application.properties里的属性里面的属性就会覆盖application.yml。

2.5多环境配置

2.5.1 profile多环境配置

程序部署到不同运行环境时,配置细节不同(如日志级别,数据源配置)。以往做法是每次发布时替换配置文件(繁琐),而springboot的profile很方便解决该问题。

1)      多环境文件名以application-{profile}.properties的格式命名,{profile}对应你的环境标识,如:

application-dev.properties:开发环境

application-prod.properties:生产环境

2)      使用对应环境,仅需在application.properties中使用spring.profile.active属性设置,值为对应的{profile}标识。

当然也可以使用命令行执行:

java -jar xxx.jar --spring.profiles.active=dev

2.5.2 注解@Profile多环境配置

除了使用profile配置文件分区配置,可注解@Profile配置,如数据库配置:

1)先定义一个接口:

public  interface DBConnector { public  void  configure(); }

2)定义2个实现类:

/**

* 测试数据库

*/

@Component

@Profile("testdb")

public class TestDBConnector implements DBConnector {

@Override

public void configure() {

System.out.println("testdb");

}

}

/**

* 生产数据库

*/

@Component

@Profile("devdb")

public class DevDBConnector implements DBConnector {

@Override

public void configure() {

System.out.println("devdb");

}

}

3)配置文件中配置

spring.profiles.active=testdb

4)使用

@RestController

@RequestMapping("/task")

public class TaskController {

@Autowired DBConnector connector ;

@RequestMapping(value = {"/",""})

public String hellTask(){

connector.configure(); //最终打印testdb

return "hello task !! myage is " + myage;

}

}

除了spring.profiles.active来激活一个或者多个profile之外,还可以用spring.profiles.include来叠加profile:

spring.profiles.active: testdb

spring.profiles.include: proddb,prodmq

springboot学习入门之二---配置文件解析的更多相关文章

  1. springboot 学习笔记(二)

    springboot 学习笔记(二) 快速创建一个springboot工程,并引入所需要的依赖 1.利用Spring initializr 来创建一个springboot项目,登陆http://sta ...

  2. SpringBoot学习笔记:读取配置文件

    SpringBoot学习笔记:读取配置文件 配置文件 在以往的项目中,我们主要通过XML文件进行框架配置,业务的相关配置会放在属性文件中,然后通过一个属性读取的工具类来读取配置信息.在SpringBo ...

  3. Java开发学习(三十五)----SpringBoot快速入门及起步依赖解析

    一.SpringBoot简介 SpringBoot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化 Spring 应用的初始搭建以及开发过程. 使用了 Spring 框架后已经简化了我 ...

  4. SpringBoot学习入门之Hello项目的构建、单元测试和热部署等(配图文,配置信息详解,附案例源码)

    前言: 本文章主要是个人在学习SpringBoot框架时做的一些准备,参考老师讲解进行完善对SpringBoot构建简单项目的学习汇集成本篇文章,作为自己对SpringBoot框架的总结与笔记. 你将 ...

  5. springboot快速入门(二)——项目属性配置(日志详解)

    一.概述 application.properties就是springboot的属性配置文件 在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring ...

  6. 01-Spring Security框架学习--入门(二)

    一.入门案例 Spring Security 自定义登录界面 通过之前的一节 01-Spring Security框架学习--入门(一)的简单演示,Spring security 使用框架自带的登录界 ...

  7. ibatis源码学习2_初始化和配置文件解析

    问题在详细介绍ibatis初始化过程之前,让我们先来思考几个问题. 1. ibatis初始化的目标是什么?上文中提到过,ibatis初始化的核心目标是构造SqlMapClientImpl对象,主要是其 ...

  8. SpringBoot学习之文件结构和配置文件

    Springboot文件结构和配置文件 转载:http://www.zslin.com/web/article/detail/11 项目文件结构 新建的Springboot项目的文件结构如下: |-c ...

  9. springboot学习入门简易版二---springboot2.0项目创建

    2 springboot项目创建(5) 环境要求:jdk1.8+ 项目结构: 2.1创建maven工程 Group id :com.springbootdemo Artifact id: spring ...

随机推荐

  1. Spring Security构建Rest服务-1203-Spring Security OAuth开发APP认证框架之短信验证码登录

    浏览器模式下验证码存储策略 浏览器模式下,生成的短信验证码或者图形验证码是存在session里的,用户接收到验证码后携带过来做校验. APP模式下验证码存储策略 在app场景下里是没有cookie信息 ...

  2. 【树】Kth Smallest Element in a BST(递归)

    题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...

  3. Disconf 学习系列之Disconf是什么?

    不多说,直接上干货! Disconf是什么 Distributed Configuration Management Platform(分布式配置管理平台) ,它是专注于各种分布式系统配置管理 的通用 ...

  4. zookeeper基础知识

    Zookeeper简介 ZooKeeper设计目的 最终一致性client不论连接到哪个Server,展示给它都是同一个视图,这是zookeeper最重要的性能. 可靠性具有简单.健壮.良好的性能,如 ...

  5. Web服务端性能提升实践

    随着互联网的不断发展,日常生活中越来越多的需求通过网络来实现,从衣食住行到金融教育,从口袋到身份,人们无时无刻不依赖着网络,而且越来越多的人通过网络来完成自己的需求. 作为直接面对来自客户请求的Web ...

  6. C#的Equals不区分大小写

    代码:System.Windows.Forms.MessageBox.Show("abc".Equals("ABC", StringComparison.Inv ...

  7. haml参考大全

    原文来自: http://blackanger.blog.51cto.com/140924/47642   Haml是一种用来描述任何XHTML web document的标记语言,它是干净,简单的. ...

  8. Rails中activeAdmin的使用

    一.开始ActiveAdmin Active Admin是一个发布在RAILS3中使用的Gem. 1.我们为了快速开始我们对Active Admin的了解,我们首先安装它: 在你GemFile中添加g ...

  9. Linux skbuff注释笔记

    SKB结构定义   /usr/src/linux/include/linux/skbuff.h sk_buff_head: struct sk_buff_head { //SKB的头结点 /* The ...

  10. [转]RDL Report in Visual Studio New page per Record

    本文转自:https://social.msdn.microsoft.com/Forums/sqlserver/en-US/f58cd5cf-4296-40f0-b3c8-7e4e15d73762/r ...