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
补充:代码也可以根据当前激活的环境来进行IOC生成对应的bean,如下:
(1)application.properties激活dev对象
spring.profiles.active=dev
(2)代码中根据激活的环境生成对象:
package com.zd.bx.service; public interface TestService { void test1(); }
dev环境的实现类:
package com.zd.bx.service; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; @Service @Profile("dev") public class DevTestService implements TestService { @Override public void test1() { System.out.println("test1 dev"); } }
prod环境的实现类:
package com.zd.bx.service; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Service; @Service @Profile("prod") public class ProdTestService implements TestService { @Override public void test1() { System.out.println("test1 prod"); } }
Profile注解可以接受一个数组,多个激活的环境。源码如下:
/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. */ package org.springframework.context.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.core.env.AbstractEnvironment; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Profiles; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional(ProfileCondition.class) public @interface Profile { /** * The set of profiles for which the annotated component should be registered. */ String[] value(); }
补充:application.properties也可以引入多个properties文件,如下
# include激活多个包
#spring.profiles.include=dev
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);