前言

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=.htmlspring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/html# set to false for hot refreshspring.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。

【spring boot】SpringBoot初学(3)– application配置和profile隔离配置的更多相关文章

  1. application配置和profile隔离配置(转)

    前言 github: https://github.com/vergilyn/SpringBootDemo 说明:我代码的结构是用profile来区分/激活要加载的配置,从而在一个project中写各 ...

  2. application配置和profile隔离配置

    前言 github: https://github.com/vergilyn/SpringBootDemo 说明:我代码的结构是用profile来区分/激活要加载的配置,从而在一个project中写各 ...

  3. 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用

    2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...

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

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

  5. Spring Boot 核心配置文件 bootstrap & application

    Spring Boot 核心配置文件 bootstrap & application 1.SpringBoot bootstrap配置文件不生效问题 2.bootstrap/ applicat ...

  6. Spring Boot中的缓存支持(一)注解配置与EhCache使用

    Spring Boot中的缓存支持(一)注解配置与EhCache使用 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决 ...

  7. Spring boot 工具类静态属性注入及多环境配置

    由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理.但是程序部署到线上生产环境后,是可以直接访问MongoDB的,因此 ...

  8. Spring Boot 静态资源映射与上传文件路由配置

    默认静态资源映射目录 默认映射路径 在平常的 web 开发中,避免不了需要访问静态资源,如常规的样式,JS,图片,上传文件等;Spring Boot 默认配置对静态资源映射提供了如下路径的映射 /st ...

  9. Spring Boot 集成 Mybatis(druid 数据库连接池 以及 分页配置)

    MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射,目前很大一部分互联网.软件公司都在使用这套框架 关于Mybatis-Generator的下载可以到这个地址:http ...

随机推荐

  1. 基于 HTML5 WebGL 的智慧楼宇可视化系统

    前言 可视化的智慧楼宇在 21 世纪是有急迫需求的,中国被世界称为"基建狂魔",全球高层建筑数量位居首位,所以对于楼宇的监控是必不可少.智慧楼宇可视化系统更多突出的是管理方面的功能 ...

  2. 快速下载Keil μVision MDK-Arm包

    搜索"MDK Pack",找到Keil官网的MDK包下载页,如下图所示 Keil的官网的域名是Keil.com,下图搜索结果的URL的域名部分被红框标记,那是Keil官网的域名 以 ...

  3. Go语言实现:【剑指offer】从尾到头打印链表

    该题目来源于牛客网<剑指offer>专题.​ 输入一个链表,按链表从尾到头的顺序返回一个ArrayList. Go语言实现: type ListNode struct { Val int ...

  4. 编辑crontab添加Linux计划任务

    在做实验楼的比赛时的题目 用到了crontable 1. 为用户shiyanlou添加一个计划任务 2. 每天凌晨2点定时执行 3. 将/var/log/dpkg.log /var/log/mysql ...

  5. k8s系列----一个简单的例子

    本实验来自k8s权威指南 解决访问demo出错,关键是靠https://www.cnblogs.com/neutronman/p/8047547.html此链接下面的某个大神的评论 主要yaml文件 ...

  6. pycharm template 设置

    ${PROJECT_NAME} - 当前Project名称; (the name of the current project. ) ${NAME} -创建文件的对话框中制定的文件名; (the na ...

  7. CentOS7时区和时间设置

    [root@saltstack-master ~]# timedatectl set-timezone Asia/Shanghai [root@saltstack-master ~]# ln -sf ...

  8. Springboot feign 传递request信息

    基础实现 requestInterceptor 实现类中添加信息 public class NativeFeignConf { @Bean public RequestInterceptor getR ...

  9. C++ 常用编程--Swap函数有几种写法?

    C++ 常用编程--Swap函数有几种写法? 在说C++模板的方法前,我们先想想C语言里面是怎么做交换的. 举个例子,要将两个int数值交换,是不是想到下面的代码: void swap(int& ...

  10. 9种分布式ID生成之 美团(Leaf)实战

    整理了一些Java方面的架构.面试资料(微服务.集群.分布式.中间件等),有需要的小伙伴可以关注公众号[程序员内点事],无套路自行领取 更多优选 一口气说出 9种 分布式ID生成方式,面试官有点懵了 ...