application配置和profile隔离配置(转)
前言
github: https://github.com/vergilyn/SpringBootDemo
说明:我代码的结构是用profile来区分/激活要加载的配置,从而在一个project中写各种spring boot的demo。所以很多时候可能在*Application.class中指定了特殊的profile。
这种方式可能很糟糕(在细看理解application.properties的加载顺序后,感觉在*Application.class目录下写application.properties应该更好。)
代码位置:
一、application.properties
1. 1 application.properties的加载顺序 (重要)
SpringApplication将从以下位置加载application.properties文件,并把它们添加到Spring Environment中:
(1) 当前目录下的一个/config子目录;
(2) 当前目录;
(3) 一个classpath下的/config包;
(4) classpath根路径(root);
这个列表是按优先级排序的(列表中位置高的将覆盖位置低的)。
1.2 application.properties 加载目录/文件名 修改
如果不喜欢将application.properties作为配置文件名,可以通过指定spring.config.name环境属性来切换其他的名称。
也可以使用spring.config.location环境属性来引用一个明确的路径(目录位置或文件路径列表以逗号分割)。(并不是在config/application.properties中指定)
二、spring boot的profile指定
2.1 什么是profile,怎么理解profile?
Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只能在特定的环境下生效。
任何@Component或@Configuration都能被@Profile标记,从而限制加载它的时机。
比如:开发环境与正式环境的数据库连接配置文件不一样,在开发服务器指定为开发配置文件,在正式环境时切换正式的配置文件。(多看代码去理解)
2.2 profile的加载顺序、格式
格式:application-{profile}.properties
加载顺序:与application.properties一样。
profile中的配置会覆盖默认的配置,包括application.properties中的配置。
2.3 profile在spring boot中的配置
(1) application.properties中指定属性配置

#### 特定配置profile,多个可用逗号(,),ex:spring.profiles.active=dev,prod
## 特定Profile属性从跟标准application.properties相同的路径加载,并且特定profile文件会覆盖默认的配置。
spring.profiles.active=dev
## 用来无条件的添加生效的配置。或 SpringApplication.setAdditionalProfiles(String... profiles)
## 可能在pom中依赖了部分jar,所以可能必须使用手动配置(否则spring boot会用jar中的自动配置,比如database)
spring.profiles.include=log,thymeleaf,db

(问题: active与include有什么区别?)
(2) 用SpringApplication中的API配置
(3) 代码中限制加载及demo


@Controller
//@Profile注解可以实现不同环境下配置参数的切换,任何@Component或@Configuration注解的类都可以使用@Profile注解。
@Profile("dev")
public class DevController {
@Value("${app.name}")
private String appName;
@Value("${app.description}")
private String appDesc; @RequestMapping("/profile")
public String greeting(
@RequestParam(value = "name", required = false, defaultValue = "VergiLyn") String name,
Model model) {
model.addAttribute("appDesc", appDesc);
model.addAttribute("name", appName);
return "greeting";
}
}




@Controller
@Profile("prod")
public class ProdController {
@Value("${app.name}")
private String appName;
@Value("${app.description}")
private String appDesc; @RequestMapping("/profile")
public String greeting(
@RequestParam(value = "name", required = false, defaultValue = "VergiLyn") String name,
Model model) {
model.addAttribute("appDesc", appDesc);
model.addAttribute("name", appName);
return "greeting";
}
}




@SpringBootApplication
public class ProfileApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(ProfileApplication.class);
app.setAdditionalProfiles("dev"); // dev 或prod
app.run(args);
} }


application-dev.properties
## 属性占位符
app.name=dev_spring_boot
application-prod.properties
## 属性占位符
app.name=prod_spring_boot
application.properties


app.name=default_spring_boot
app.description=${app.name} is a Spring Boot application
## thymeleaf 配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
# set to false for hot refresh
spring.thymeleaf.cache=false
## dev/prod,此demo在java代码中指定
# spring.profiles.active=dev
# spring.profiles.include=...


greeting.html


<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>


结果:
如果profile启用”dev”,则app.name=dev_spring_boot,访问localhost:8080/profile被加载注入的bean是DevController。
application配置和profile隔离配置(转)的更多相关文章
- application配置和profile隔离配置
前言 github: https://github.com/vergilyn/SpringBootDemo 说明:我代码的结构是用profile来区分/激活要加载的配置,从而在一个project中写各 ...
- 【spring boot】SpringBoot初学(3)– application配置和profile隔离配置
前言 github: https://github.com/vergilyn/SpringBootDemo 说明:我代码的结构是用profile来区分/激活要加载的配置,从而在一个project中写各 ...
- 使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置
引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发.测试.生产)配置参数的切换 一.根据springboot的配置文件命名约定,结合ac ...
- Nacos(六):多环境下如何“管理”及“隔离”配置和服务
前言 前景回顾: Nacos(五):多环境下如何"读取"Nacos中相应环境的配置 Nacos(四):SpringCloud项目中接入Nacos作为配置中心 现如今,在微服务体系中 ...
- spring boot 环境配置(profile)切换
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- 切换用户后,/etc/profile的配置不起效
遇到的问题 在配置linux的时候,发现一个问题:su root切换到root用户后,/etc/profile 中配置的PATH不起效果. 问题分析和疑问 是不是~/.profile,~/.bashr ...
- Linux 系统/etc/profile 内配置 系统脚本命令
背景 在Linux系统下,我们需要利用脚本命令启动一个进程的时候,需要先找到找到启动文件,然后再启动.比如服务器上安装了一个was应用服务器,我们需要每次启动服务器都需要使用如下命令: sh was ...
- PHP 网站隔离配置
PHP网站间隔离 网站内目录与目录之间是可以访问的,在某些特定情况下这样是不安全的,如果目录间网址权限被黑客利用很可能造成数据流失,在这里我们可以通过PHPopen_basedir来实现网站间目录隔离 ...
- 在/etc/profile下配置java的环境变量
在/etc/profile下配置java的环境变量 原创 Java 作者:xiaoyan5686670 时间:2016-01-18 14:30:28 6152 0 以root用户编辑:#vi /e ...
随机推荐
- Python取得系统进程列表
一.上代码 import psutil for proc in psutil.process_iter(): try: pinfo = proc.as_dict(attrs=['pid', 'name ...
- Spring Jdbc事例说明(三)
上一篇文章已经讲解了如何使用Spring搭建工程,这一篇文章是接着上一篇文章来描述的. 一.载入依赖 新增加了两个依赖,mysql数据库驱动和alibaba数据源包 <!-- mysql驱动包 ...
- java动态代理_aop2
一.什么是代理? 代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为委托类预处理消息,过滤消息并转发消息,以及进行消息被委托类执行后的后续处理. 代理模 ...
- Git如何获得两个版本间所有变更的文件列表
https://segmentfault.com/q/1010000000133613 git diff --name-status HEAD~2 HEAD~3
- Percona-XtraBackup系列一:安装 perl(Time::HiRes) is needed by percona-xtrabackup-2.2.10-1.el6.x86_64
1:在percona官网下载最新的Xtrabackup http://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.2.10/bi ...
- 在openerp撰写消息中增加图片
openerp的撰写消息中, 在文本输入框中, 具有设置文本字体,设置对齐方式 等多种功能, 就像像写这篇新浪blog一样, 可以输入富文本信息. 美中不足的是, 它不能插入图片. 我们如何才能让op ...
- [转]jquery设置select选中,赋值等操作
一.基础取值问题 例如<select class="selector"></select> 1.设置value为pxx的项选中 $(".selec ...
- visual studio运行时库MT、MTd、MD、MDd的研究
在开发window程序是经常会遇到编译好好的程序拿到另一台机器上面无法运行的情况,这一般是由于另一台机器上面没有安装响应的运行时库导致的,那么这个与编译选项MT.MTd.MD.MDd有什么关系呢?这是 ...
- IM系统架构设计之浅见
转自:http://mobile.51cto.com/hot-439693.htm 背景:除去大名鼎鼎的QQ这款即时聊天工具,还有许多细分行业的IM,比如淘宝阿里旺旺.网易泡泡.YY语音....... ...
- 如何用命令行执行loadrunner的脚本
SET M_ROOT=D:\Mercury Interactive\Mercury LoadRunner\bin cd %M_ROOT% wlrun.exe -TestPath D:\ceshi10\ ...