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);
原文地址:https://www.cnblogs.com/qlqwjy/p/10552635.html