一、配置文件application

  springboot配置文件,默认配置文件application.propertie或者application.yml,可同时存在。

  基础使用

    application.propertie增加配置:local.ip=192.168.1.1

    application.yml增加配置【使用缩进】:

jdbc:
name: lhx

  默认位置:classpath、classpath:/config、file:/、file:config下

  注意:application.properties==application-default.properties

1.1、读取方式

方式一、Environment方式读取

context.getEnvironment().getProperty("local.ip","默认值")

@SpringBootApplication
public class App {
@Bean
public Runnable createRunnable() {
return () -> {
System.out.println("spring boot is running");
};
} public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
context.getBean(Runnable.class).run();
System.out.println(context.getEnvironment().getProperty("local.ip"));
context.close();
}
}

其实相当于自定义注入Environment ,然后使用env.getProperty("local.ip")即可

@Component
public class UserConfig { @Autowired
private Environment env; public void show() {
System.out.println("local.ip=" + env.getProperty("local.ip"));
}
}

另一种设置默认值方式

        HashMap<String, Object> defaultProperties = new HashMap<>();
defaultProperties.put("server.host", "127.0.0.1");
application.setDefaultProperties(defaultProperties);

方式二、使用@Value注解

    @Value("${local.port}")
private String localPort;

默认必须有配置项,如果没有配置项可以增加默认值

    @Value("${tomcat.port:9090}")
private String tomcatPort2;

这种的:@Value("tomcat.port") 也可以使用。具体没查

方式三、使用@ConfigurationProperties(prefix="ds")

在application配置文件中增加ds.url=jdbc:mysql://spring

增加读取类

@Component
@ConfigurationProperties(prefix="ds")
public class DataSourceProperties {
private String url;
public void show() {
System.out.println("url"+url);
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
} }

其他处使用即可。

注意:@ConfigurationProperties(prefix="ds")也支持指定具体路径文件配置@ConfigurationProperties(prefix="ds",localtions="classpath:/ds.properties")

注入数组、集合,在配置文件中增加

ds.host[]=192.168.1.1
ds.host[]=192.168.1.2
ds.host[]=192.168.1.3

代码中

private List<String> hosts = new ArrayList<>();

即可,注意增加getter,setter,

注:支持配置引用配置

name=springboot
aap.name=this is ${name}

1.2、指定具体名称配置

如不使用application.properties,改为app.proerties.

方式一、启动参数中修改:

--spring.config.name=app

或者 有路径的 --spring.config.location=classpath:cong/app.propertis

或者 多个用逗号隔开

或者 file目录    --spring.config.location=classpath:cong/app.propertis,file:E:/app.properties

方式二、文件注解@PropertySource

增加jdbc.properties配置文件

增加Config配置类

@Configuration
@PropertySource("classpath:jdbc.properties")
public class FileConfig { }

然后使用即可 PropertySource 可以列多个

或者多个可以使用 @PropertySources({@PropertySource("classpath:jdbc.properties")})

@PropertySource: 用于引入外部属性配置,和Environment 配合一起使用。其中ignoreResourceNotFound 表示没有找到文件是否会报错,默认为false,就是会报错,一般开发情况应该使用默认值,设置为true相当于生吞异常,增加排查问题的复杂性.

引入PropertySource,注入Environment,然后就能用environment 获取配置文件中的value值。

二、EnvironmentPostProcessor配置文件扩展

需要注册到META-INF/spring.factories文件

1.增加此文件,并增加内容

org.springframework.boot.env.EnvironmentPostProcessor=com.lhx.spring.springboot_config.MyEnvironmentPostProcessor

2.增加实现类文件MyEnvironmentPostProcessor

三、Profiles

增加两个配置文件

方式一、程序读取

在application-dev.properties中添加

jdbc.url=jdbc:mysql://127.0.0.1/spring_dev

在application-test.properties中添加

jdbc.url=jdbc:mysql://127.0.0.1/spring_test

程序使用

        SpringApplication app = new SpringApplication(App3.class);
app.setAdditionalProfiles("test");//test 读取application-test.properties
ConfigurableApplicationContext context = app.run(args);
context.getBean(Runnable.class).run();
System.out.println(context.getEnvironment().getProperty("jdbc.url"));
context.close();

注:可在setAdditionalProfiles配置多个,会被覆盖

方式二、参数配置

启动参数增加,多个使用逗号分割,配置多个 多个同时生效

--spring.profiles.active=test

使用

执行java -jar xxx.jar,可以观察到服务端口被设置为8001,也就是默认的开发环境(dev)

执行java -jar xxx.jar --spring.profiles.active=test,可以观察到服务端口被设置为8002,也就是测试环境的配置(test)

执行java -jar xxx.jar --spring.profiles.active=prod,可以观察到服务端口被设置为8003,也就是生产环境的配置(prod)

总结多环境的配置思路:

  application.properties中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认配置

  application-{profile}.properties中配置各个环境不同的内容

  通过命令行方式去激活不同环境的配置

方式三、@Profile注解

@SpringBootConfiguration
public class MyConfig {
@Bean
public Runnable createRunnable() {
System.out.println("--------1--------");
return ()->{};
} @Bean
@Profile("dev")
public Runnable createRunnable2() {
System.out.println("--------2--------");
return ()->{};
} @Bean
@Profile("test")
public Runnable createRunnable3() {
System.out.println("--------3--------");
return ()->{};
}
}

启动对应环境时候生效

 

005-Spring Boot配置分析-配置文件application、EnvironmentPostProcessor、Profiles的更多相关文章

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

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

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

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

  3. Spring Boot配置,读取配置文件

    Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...

  4. Spring Boot 启动(四) EnvironmentPostProcessor

    Spring Boot 启动(四) EnvironmentPostProcessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698. ...

  5. Spring boot配置MongoDB以及Morphia踩坑记录

    pom 因为项目中采用Morphia(MongoDB的ODM框架,对象-文档映射(object-document mapper)),因此需要在pom文件中引入相应依赖: <dependency& ...

  6. Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置

    Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...

  7. Spring Boot -- 配置切换指南

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  8. Spring Boot 配置优先级顺序

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  9. 笔记:Spring Boot 配置详解

    Spring Boot 针对常用的开发场景提供了一系列自动化配置来减少原本复杂而又几乎很少改动的模板配置内容,但是,我们还是需要了解如何在Spring Boot中修改这些自动化的配置,以应对一些特殊场 ...

随机推荐

  1. Activiti6-数据库配置-dbconfig(学习笔记)

    常用数据连接池种类: 不一样的地方在于filters过滤器,设置了统计.和记录 avtiviti支持的数据库有: <?xml version="1.0" encoding=& ...

  2. Debugging Beyond Visual Studio – WinDbg

    Getting started with WinDbg: 1. Download the Debugging Tools for Windows from the Microsoft website ...

  3. springdata jpa 原始sql的使用

  4. 【题解】放球游戏A

    题目描述 校园里在上活动课,Red和Blue两位小朋友在玩一种游戏,他俩在一排N个格子里,自左到右地轮流放小球,每个格子只能放一个小球.每个人一次只能放1至5个球,最后面对没有空格而不能放球的人为输. ...

  5. kubernetes 安装metrics-server

    metrics-server文件下载: https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/metrics-serv ...

  6. Html | Vue | Element UI——引入使用

    前言 做个项目,需要一个效果刚好Element UI有,就想配合Vue和Element UI,放在tp5.1下使用,但是引入在线的地址各种报错,本地引入就完美的解决了问题! 代码 __STATIC_J ...

  7. Django_RBAC_demo2 升级版权限控制组件

    RBAC 升级版 预期要求 前端在无权限时不在提供操作标签 更改部分硬编码 实现更加精准的权限控制 未改动前的版本 在这里 ⬇ Django_rbac_demo 权限控制组件框架模型 具体更改 数据库 ...

  8. [CTSC2018]暴力写挂

    题目描述 www.lydsy.com/JudgeOnline/upload/201805/day1(1).pdf 题解 首先来看这个我们要最大化的东西. deep[u]+deep[v]-deep[lc ...

  9. 记录一次被bc利用跳转过程分析

    挖公司的项目站,发现站点一访问就直接跳转到了赌博站,有点懵逼,简单分析下hc利用过程: 公司项目站:http://***.com 当我访问它: 通过http:***.com直接跳转到了306648.c ...

  10. Centos 7最小化部署apollo

    https://github.com/nobodyiam/apollo-build-scripts