application.properties

  application.properties是spring boot默认的配置文件,spring boot默认会在以下两个路径搜索并加载这个文件

    src\main\resources

    src\main\resources\config

配置系统参数

  在application.properties中可以配置一些系统参数,spring boot会自动加载这个参数到相应的功能,如下

#端口,默认是8080
server.port=8088
#访问路径,默认为/
server.servlet.context-path=/test

测试:http://localhost:8088/test/hello

推荐使用yaml文件,后缀为yml

server:
port: 8088
servlet:
context-path: /test

注意冒号:后面有个空格 好处就是简洁  不用重复的写前缀 只保留一种即可,删掉properties

applicaiton.properties配置中文值的时候读取的时候会出现乱码问题,而yml不会

如果在定义user.name=xxx的时候也是读取不到的,因为spring boot首先会加在sytemEnvironment作为首个PropertySource mac会返回自己的登录账号

自定义properties文件

  在spring boot启动类或配置类中添加以下注解,可再启动时载入自定义的配置文件

@PropertySource("classpath:config/xxx.properties")

  如果要同时载入多个文件就用数组

@PropertySource(value={"classpath:config/a.properties","classpath:config/b.properties"})

自定义参数

新增a.properties

key1=values1
key2=values2

然后再java代码中使用@Value注解,在项目启动时会将自定义参数加载到全局变量。

@RestController //引入spring boot的web模块,就会自动配置web.xml等于web相关的内容
@PropertySource("classpath:a.properties")
public class HelloController { @Autowired
String testStr; @Value("${key1}")
String key1; @RequestMapping("/hello")
public String hello(){
return testStr+":"+key1;
}
}

访问:http://localhost:8088/test/hello

显示:Hello World:values1

批量注入到类变量

  在a.properties中配置几个以user为前缀的参数

key1=values1
key2=values2
user.Id=4
user.userName=shabaidaweia
user.age=183

  在java中用@ConfigurationProperties 将以user为前缀的参数注入到当前类的变量中,需要有set方法。

package com.spring.boot.utils;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "user")
public class UserProperties {
private Integer Id; public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} private String userName;
private String age; public Integer getId() {
return Id;
} public void setId(Integer id) {
Id = id;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
}
}

在controller中使用配置文件即可

@RestController
public class HelloController { @Autowired
private UserProperties userProperties; @GetMapping("/name")
public String name(){
return userProperties.getUserName();
}
}

参数引用

  在properties中的各个参数之间也可以相互引用如下:

com.xxx.name=david
com.xxx.age=18
com.xxx.sayHi=${com.xxx.name}今年${com.xxx.age}岁了

多环境配置

  我们在开发spring boot时,通常一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口、支付账号等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常痛苦的事情。

  在spring boot中多环境配置文件名需要满足 application-{profile}.properties格式,其中{profile}对应你的环境标识:

  application-dev.properties 开发环境

  application-test.properties 测试环境

  application-prod.properties 生产环境

具体哪个配置文件会被加在,需要在application.properties中通过spring.profiles.active属性来设置,其值对应{profile}值。

如 spring.profiles.active=test 就会加在test.properties配置文件的内容

application.yml

spring:
profiles:
active: dev #指定读取哪个文件

application-dev.yml

server:
port: 8088
servlet:
context-path: /dev

application-prod.yml

server:
port: 8088
servlet:
context-path: /

application-test.yml

server:
port: 8088
servlet:
context-path: /test

启动项目:http://localhost:8088/dev/name

通过命令行设置属性值

通过java -jar xx.jar 启动项目时 可以输入 java -jar xx.jar --server.port = 8888 ,来设置xxx.jar对应的端口。

在命令行运行时,连续的两个减号 -- 就是对application.properties属性值进行赋值,所以这个命令等价于我们在application.properties中设置server.port=8888。

可以设置SpringApplication.setAddCommandLineProperties(false)。来进行屏蔽命令行设置参数

打开终端:进入项目目录,执行 mvn package 打包

或者在idea下方 Terminal中输入

打包完成后 进入target目录中,发现一个名为boot.war包

输入命令 java -jar boot-0.0.1-SNAPSHOT.war

访问:http://localhost:8088/dev/name    shabaidaweia

再打开一个终端:

java -jar boot-0.0.1-SNAPSHOT.war --user.username=666  --server.port=8888

访问:http://localhost:8888/dev/name

显示:666

Spring Boot (5) Spring Boot配置详解的更多相关文章

  1. 【Spring】——声明式事务配置详解

    项目中用到了spring的事务: @Transactional(rollbackFor = Exception.class, transactionManager = "zebraTrans ...

  2. Spring学习(十九)----- Spring的五种事务配置详解

    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...

  3. spring, spring mvc, mybatis整合文件配置详解

    转自:http://www.cnblogs.com/wxisme/p/4924561.html 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用 ...

  4. Spring Boot 2.0 教程 - 配置详解

    Spring Boot 可以通过properties文件,YAML文件,环境变量和命令行参数进行配置.属性值可以通过,@Value注解,Environment或者ConfigurationProper ...

  5. spring boot slf4j日记记录配置详解

    https://blog.csdn.net/liuweixiao520/article/details/78900779

  6. Spring MVC的web.xml配置详解(转)

    出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在w ...

  7. Spring mvc的web.xml配置详解

    1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(l ...

  8. 《Java Spring框架》Spring切面(AOP)配置详解

    1.  Spring 基本概念 AOP(Aspect Oriented Programming)称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2 ...

  9. 1、Spring MVC的web.xml配置详解(转)

    版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilt ...

  10. Spring中配置文件applicationContext.xml配置详解

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

随机推荐

  1. [工具]iostat

    本文主要分析了Linux的iostat命令的源码 iostat源码共563行,应该算是Linux系统命令代码比较少的了.源代码中主要涉及到如下几个Linux的内核文件: 1./proc/disksta ...

  2. vue-router的基本使用和配置

    1.在main.js文件中引入相关模块以及组件及实例化vue对象配置选项路由及渲染App组件 默认设置如下: import Vue from 'vue' import App from './App' ...

  3. JS布尔值(Boolean)转换规则

    原文作者: louis 原文链接: http://louiszhai.github.io/2015/12/11/js.boolean/ 语法 众所周知, JavaScript有五个基本的值类型:num ...

  4. python--(十五步代码学会进程)

    python--(十五步代码学会进程) 一.进程的创建 import time import os #os.getpid() 获取自己进程的id号 #os.getppid() 获取自己进程的父进程id ...

  5. BZOJ 1617 Usaco 2008 Mar. River Crossing渡河问题

    [题解] 显然是个DP题. 设$f[i]$表示送$i$头牛过河所需的最短时间,预处理出$t[i]$表示一次性送i头牛过河所需时间,那么我们可以得到转移方程:$f[i]=min(f[i],f[i-j]+ ...

  6. 【[Offer收割]编程练习赛14 D】剑刃风暴(半径为R的圆能够覆盖的平面上最多点数目模板)

    [题目链接]:http://hihocoder.com/problemset/problem/1508 [题意] [题解] 求一个半径为R的圆能够覆盖的平面上的n个点中最多的点数; O(N2log2N ...

  7. Leetcode 42.接雨水

    接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下 ...

  8. Istio是啥?一文带你彻底了解!

    原标题:Istio是啥?一文带你彻底了解! " 如果你比较关注新兴技术的话,那么很可能在不同的地方听说过 Istio,并且知道它和 Service Mesh 有着牵扯. 这篇文章可以作为了解 ...

  9. js移除style属性

    这个属性是通过   div.style.color="red"   这种类似添加的,想要添加重置函数,使用div.removeAttribute("style" ...

  10. Oracle Auto Increment Column - Sequence as Default Value

        Solution 1: Prior to Oracle 11g, sequence assignment to a number variable could be done through ...