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; @Override
public 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);

springboot中spring.profiles.active来引入多个properties文件 & Springboot获取容器中对象的更多相关文章

  1. SpringBoot利用spring.profiles.active=@spring.active@不同环境下灵活切换配置文件

    一.创建配置文件 配置文件结构:这里建三个配置文件,application.yml作为主配置文件配置所有共同的配置:-dev和-local分别配置两种环境下的不同配置内容,如数据库地址等. appli ...

  2. spring-boot:run启动时,指定spring.profiles.active

    Maven启动指定Profile通过-P,如mvn spring-boot:run -Ptest,但这是Maven的Profile. 如果要指定spring-boot的spring.profiles. ...

  3. springboot中spring.profiles.include

    springboot中spring.profiles.include的妙用. 我们有这样的一个springboot项目.项目分为开发.测试.生产三个不同阶段(环境),每个阶段都会有db.ftp.red ...

  4. springboot中spring.profiles.include的妙用

    我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测试.生产等.其中每个环境的数据库地址.服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改 ...

  5. SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active

    趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...

  6. SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active(转)

    趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...

  7. spring程序打包war,直接通过-jar启动,并指定spring.profiles.active参数控制多环境配置

    备注:spring boot有内嵌tomcat,jar项目可以用java -jar命令启动,war包也可以,且可以直接指定spring.profiles.active参数控制多环境配置 直接指定传参, ...

  8. 使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置

    引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发.测试.生产)配置参数的切换 一.根据springboot的配置文件命名约定,结合ac ...

  9. spring boot 入门 使用spring.profiles.active来分区配置

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

随机推荐

  1. 简单贪心) Repair the Wall hdu2124

    Repair the Wall http://acm.hdu.edu.cn/showproblem.php?pid=2124 Time Limit: 5000/1000 MS (Java/Others ...

  2. printf不定参数

    title: printf不定参数 tags: C ARM date: 2018-10-21 12:14:58 --- 不定参数的传递 函数调用时参数传递是使用堆栈来实现的,参数入栈顺序是从右向左,在 ...

  3. java 中数据的强制转换 和计算的补码运算

    原码 反码 补码的定义与运算 1原码: 原码是将十进制或者其他进制的数转换为二进制表示(且要根据数据的类型转换) 如:130 (默认是Int类型,则是4个字节) 原码是:00000000 000000 ...

  4. 2、JPA-Annotation

    注解放在类属性上不生效时可放在get方法上试试,原因未知 @Entity /** * @Entity 该Java类为实体类,将映射到数据库表.如声明一个实体类 Customer,它将映射到数据库中的 ...

  5. 面向对象+jquery实现拖拽功能

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. log4j日志文件名与行号显示乱码? 问号? 参数问号? 日志问号?【转】【补】

    log4j本来设置了要打印行号与文件名的,结果有的能打印出来,有的却是乱码,查了些文档之后才发现,原来打印问题是因为编绎时没有编绎进去调试信息,所以没办法打印,好像有的系统又会显示(Unknown S ...

  7. tedu训练营day03

    Day03笔记1.作业 1.假如你现在25周岁,每年365天,计算你过了多少个星期天(大概数字) 提示 :地板除 2.毕业薪资为10000元,每年涨20%,十年之后你的薪资为多少元? 提示: 幂运算( ...

  8. ajax方式下载文件

    在web项目中需要下载文件,由于传递的参数比较多(通过参数在服务器端动态下载指定文件),所以希望使用post方式传递参数.通常,在web前端需要下载文件,都是通过指定<a>标签的href属 ...

  9. jsp实现验证码登陆

    login.jsp: <%@ page language="java" import="java.util.*,com.cn.servlet.*" pag ...

  10. ASP.NET MVC 3 Razor 语法

    1.   三元运算符 1)   输出文本 1.   View var var1 = '@(1 < 2 ? "YES" : "NO")'; var var2 ...