springboot中spring.profiles.active来引入多个properties文件 & Springboot获取容器中对象
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获取容器中对象的更多相关文章
- SpringBoot利用spring.profiles.active=@spring.active@不同环境下灵活切换配置文件
一.创建配置文件 配置文件结构:这里建三个配置文件,application.yml作为主配置文件配置所有共同的配置:-dev和-local分别配置两种环境下的不同配置内容,如数据库地址等. appli ...
- spring-boot:run启动时,指定spring.profiles.active
Maven启动指定Profile通过-P,如mvn spring-boot:run -Ptest,但这是Maven的Profile. 如果要指定spring-boot的spring.profiles. ...
- springboot中spring.profiles.include
springboot中spring.profiles.include的妙用. 我们有这样的一个springboot项目.项目分为开发.测试.生产三个不同阶段(环境),每个阶段都会有db.ftp.red ...
- springboot中spring.profiles.include的妙用
我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测试.生产等.其中每个环境的数据库地址.服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改 ...
- SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active
趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...
- SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active(转)
趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...
- spring程序打包war,直接通过-jar启动,并指定spring.profiles.active参数控制多环境配置
备注:spring boot有内嵌tomcat,jar项目可以用java -jar命令启动,war包也可以,且可以直接指定spring.profiles.active参数控制多环境配置 直接指定传参, ...
- 使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置
引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发.测试.生产)配置参数的切换 一.根据springboot的配置文件命名约定,结合ac ...
- spring boot 入门 使用spring.profiles.active来分区配置
很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同的环境 ...
随机推荐
- Redis的主从复制的原理介绍
redis主从复制 和Mysql主从复制的原因一样,Redis虽然读取写入的速度都特别快,但是也会产生读压力特别大的情况.为了分担读压力,Redis支持主从复制,Redis的主从结构可以采用一主多从或 ...
- (线性dp,LCS) POJ 1458 Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65333 Accepted: 27 ...
- VM中的Linux如何设置共享文件夹
1.点击[编辑虚拟机设置]-[选项]-[共享文件夹],选择“总是启用” 2.点击[确定],并重启系统,已经设置好了
- 读写锁ReadWriteLock
为了提高性能,Java提供了读写锁,在读的地方使用读锁,在写的地方使用写锁,灵活控制,如果没有写锁的情况下,读是无阻塞的,在一定程度上提高了程序的执行效率. Java中读写锁有个接口java.util ...
- python mysql安装&&简单基础sql
##############总结############## 1.mysql 介绍 Mysql是开源的,所以你不需要支付额外的费用. Mysql支持大型的数据库.可以处理拥有上千万条记录的大型数据库. ...
- python第一次周末大作业
#############################作业############################# 1. 三次登录验证 完成用户登录验证 要求: 1. 系统自动生成4位随机数. ...
- Ubuntu 云服务器上部署自己的 Rails 应用
自学rails一段时间了,之前只用heroku部署了网站,想尝试把网站以一个更“正经”的方式呈现出来,就买了一个阿里云服务器.参考了网上部分rails部署教程,过程中也遇到了一些问题,所以在完成之后总 ...
- js方法参数中含有单引号双引号的处理
最近在做项目时,遇到一个问题.当在js脚本中,拼接生成一个tr,然后添加到一个表格里. //假定testval是从后台传过来的数据 var testval = "含有'半角单引号的字符串&q ...
- Idea构建maven项目
Idea构建maven项目: 步骤一: 步骤二: 自动导入Maven项目: 步骤三:增加web 二:搭建spring项目结构: 结构图: 网上都是一大堆的:自己也可以去搜:ssm pom.xml ...
- 059、安装配置flannel(2019-03-28 周四)
参考https://www.cnblogs.com/CloudMan6/p/7424858.html build flannel flannel 没哟现成的执行文件可用,必须自己build,最 ...