1.    引入多个properties文件

  很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境下配置用不同的配置文件或者不同的配置

  spring boot允许你通过命名约定按照一定的格式(application-{profile}.properties)来定义多个配置文件,然后通过在application.properyies通过spring.profiles.active来具体激活一个或者多个配置文件,如果没有没有指定任何profile的配置文件的话,spring boot默认会启动application-default.properties。

  profile的配置文件可以按照application.properties的放置位置一样,放于以下四个位置,

当前目录的 “/config”的子目录下
当前目录下
classpath根目录的“/config”包下
classpath的根目录下

常见的应用场景

1.    多环境切换

在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:
  application-dev.properties:开发环境
  application-test.properties:测试环境
  application-prod.properties:生产环境

我们在总的applications.properties文件中可以通过下面切换:

spring.profiles.active=dev

2.    我们进行分模块开发的时候如下:

  在dao层的模块中有下面配置:    application-dao.properties

内容如下:

############################################################
#
# Mybatis settings
#
############################################################
#jiazai mybatis peizhiwenjian(**代表任意目录,*代表任意多个字符)
mybatis.mapper-locations = classpath:mapper/**/*Mapper.xml
mybatis.config-location = classpath:mybatis/SqlMapConfig.xml
mybatis.type-aliases-package = cn.qlq.bean############################################################
#
# datasource settings
#
############################################################
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456

我们在总的applications.properties文件可以通过下面方式引入上面properties文件:

spring.profiles.active=dao

2.获取容器中对象

  直接像在spring中获取会NPE异常。需要改装成下面工具类:

package cn.qs.utils;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;@Component
public class SpringBootUtils implements ApplicationContextAware {private static ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {SpringBootUtils.applicationContext = applicationContext;}public static Object getBean(String beanName) {return applicationContext.getBean(beanName);}public static <T> T getBean(Class<T> beanClass) {return applicationContext.getBean(beanClass);}public static <T> T getBean(String beanName, Class<T> beanClass) {return applicationContext.getBean(beanName, beanClass);}
}

进一步封装成如下工具类:

package cn.qs.utils;import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;public class SystemUtils {private SystemUtils() {}
public static <T> T getContextBean(Class<T> clazz) {WebApplicationContext currentWebApplicationContext = ContextLoader.getCurrentWebApplicationContext();T bean = currentWebApplicationContext.getBean(clazz);// 根据类型获取对象return bean;}
}

使用方法:

UserHealthService userHealthService = SpringBootUtils.getBean(UserHealthService.class);

转载于:https://www.cnblogs.com/qlqwjy/p/10552635.html

springboot中spring.profiles.active来引入多个properties文件 Springboot获取容器中对象相关推荐

  1. SpringBoot配置文件中spring.profiles.active配置详解

    SpringBoot配置文件中spring.profiles.active配置详解 1.多环境配置 我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测 ...

  2. Springboot配置文件-spring.profiles.active

    Springboot配置文件-spring.profiles.active 配置文件中spring.profiles.active是区分不同环境的配置文件. 一个springBoot项目生成会经历开发 ...

  3. springboot启动spring.profiles.active和spring.profiles.include影响的区别

    application.properties文件内容 spring.profiles.active=test4 pring.profiles.include=test3,test5 version=1 ...

  4. springBoot配置spring.profiles.active多环境

    spring.profiles.active多环境配置 springBoot动态配置多环境(dev/test/prod)数据信息读取 pom文件配置 YAML文件配置 springBoot动态配置多环 ...

  5. springboot配置spring.profiles.active多环境支持

    server:port: 8081 --- server:port: 8083 spring:profiles: dev 启动类中多了一个div 也可以这样运行

  6. springboot获取active_springboot 使用spring.profiles.active 区分不同环境下配置文件

    一.新建一个maven 工程 二.在pom.xml文件中加入如下依赖 4.0.0 com.bt.com.cn bt-springboot 0.0.1-SNAPSHOT bt-springboot bt ...

  7. spring.profiles.include 跟 spring.profiles.active 的区别

    spring.profiles.include 跟 spring.profiles.active 的区别 背景 我们经常看到springboot的 spring.profiles.active,有时也 ...

  8. springboot --spring.profiles.active=test 不生效

    在springboot项目中,运行jar包时使用 java -jar xxx.jar --spring.profiles.active=test ,但是并没有走bootstrap-test.yml文件 ...

  9. springboot:spring.profiles.active

    使用maven-install命令导出jar包. 使用 "java -jar jar包名称" 运行jar包 java -jar   springbootmvcproject-0.0 ...

  10. --spring.profiles.active=prod 无效问题

    1.问题产生背景 这周在linux环境下用jar 部署springboot项目的时候遇到了一个问题,使用以下命令启动项目时候: nohup java -jar XXX.jar  --spring.pr ...

最新文章

  1. CSDN湘苗培优|成长,从走出舒适区开始
  2. 项目怎么部署到服务器上_项目开发学习 云服务器的部署
  3. HDU 1232:流问题(并检查集合)
  4. Objective-C MacOS以管理员权限运行程序
  5. Openjudge NOI题库 ch0111/04 网线管理
  6. 开学一月,可以自己玩自己做的游戏了!!!
  7. Centos7下搭建LAMP环境,安装wordpress(不会生产博客,只是一名博客搬运工)(菜鸟)
  8. JavaScript this 关键字
  9. 让WordPress达到最高性能的13个优化技巧
  10. 写给 Python 开发者的 10 条机器学习建议
  11. 护航敏捷开发和运维 BCS2020举办DevSecOps论坛
  12. C++ RTTI 简介
  13. T5: Linux查看cpu信息,是否支持avx2指令集
  14. AcWing 2019. 拖拉机
  15. 腾讯云离线语音识别sdk
  16. 报错:Unable to check if JNs are ready for formatting
  17. java备忘--20190828
  18. python取出一组数中的奇偶数
  19. CentOS下连VisualSVN服务器时报Key usage violation错误的解决方案
  20. 简单说 一道JS闭包面试题

热门文章

  1. 升级ssl后ssh登录失效_centos7升级openssl、openssh常见问题及解决方法
  2. Ubuntu安装RabbitMQ及RabbitMQ基于使用
  3. Hibernate的单向1-1关联(二)
  4. 【渝粤教育】电大中专成本会计 (2)_1作业 题库
  5. Pandas系列(四)数据统计函数
  6. gcd前缀和-蒜头君的数轴
  7. js简单正则表达式验证密码
  8. require-ensure
  9. python练习题4
  10. 使用 IntelliJ IDEA打包Spark应用程序