介绍
这篇文章将为大家详细讲解有关使用弹簧引导实现档案动态切换,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
具体做法:
<强> 1,首先在pom中添加配置文件:强>
& lt; profiles> & lt; profile> & lt; id> dev & lt; activation> & lt; activeByDefault> true & lt;/activation> & lt; properties> & lt; spring.profiles.active> dev & lt;/properties> & lt; dependencies> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-devtools & lt; optional> true & lt;/dependency> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-undertow & lt;/dependency> & lt;/dependencies> & lt;/profile> & lt; profile> & lt; id> prod & lt; dependencies> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-undertow & lt;/dependency> & lt;/dependencies> & lt; properties> & lt; spring.profiles.active> prod & lt;/properties> & lt;/profile> & lt;/profiles>
dev指开发模式,戳指生产模式,如需其他模式,只需要添加配置文件即可。
<强> 2,在砰的一声。xml的构建中添加插件:强>
& lt; plugin> & lt; groupId> org.apache.maven.plugins & lt; artifactId> maven-resources-plugin & lt; version> $ {maven-resources-plugin.version} & lt;/version> & lt; executions> & lt; execution> & lt; id> default-resources & lt; phase> validate & lt; goals> & lt; goal> copy-resources & lt;/goals> & lt; configuration> & lt; outputDirectory>目标/classes & lt; useDefaultDelimiters> false & lt; delimiters> & lt; delimiter> # & lt;/delimiter> & lt;/delimiters> & lt; resources> & lt; resource> & lt; directory> src/main/资源/& lt;/directory> & lt; filtering> true & lt; includes> & lt; include> * */* .xml & lt; include> * */* .yml & lt;/includes> & lt;/resource> & lt; resource> & lt; directory> src/main/资源/& lt;/directory> & lt; filtering> false & lt; excludes> & lt; exclude> * */* .xml & lt; exclude> * */* .yml & lt;/excludes> & lt;/resource> & lt;/resources> & lt;/configuration> & lt;/execution> & lt;/executions> & lt;/plugin>
该配置用来在打包的时候修改配置文件。
<强> 3,编写DefaultProfileUtil工具类来添加默认启动配置文件:强>
进口org.springframework.boot.SpringApplication; 进口org.springframework.core.env.Environment; 进口java.util.HashMap; 进口java.util.Map;/* * *实用程序类加载Spring配置文件作为违约 *当没有& lt; code> spring.profiles.active设置环境中或作为命令行参数。 *如果值不可用& lt; code> application.yml然后& lt; code> dev</code>概要文件将被用作违约。 */最后公共类DefaultProfileUtil { 私有静态最终字符串SPRING_PROFILE_DEFAULT=皊pring.profiles.default"; 私人DefaultProfileUtil () { }/* * *设置一个默认使用当没有配置文件配置。 * * @param应用程序的spring应用程序 */公共静态孔隙addDefaultProfile (SpringApplication应用){ Object> Map<字符串;,defProperties=new HashMap<的在();/* *默认概要文件时要使用任何其他配置文件定义 *这个不能集& lt; code> application.yml文件。 *参见https://github.com/spring-projects/spring-boot/issues/1219 */defProperties。把(SPRING_PROFILE_DEFAULT Constants.SPRING_PROFILE_DEVELOPMENT); app.setDefaultProperties (defProperties); System.out.println(应用); }/* * *获得其他应用的概要文件的默认配置文件。 */公共静态String [] getActiveProfiles(环境env) { String[]资料=env.getActiveProfiles (); 如果配置文件。长度==0){ 返回env.getDefaultProfiles (); } 返回配置文件; } }
公共类常量 公共静态最终字符串SPRING_PROFILE_DEVELOPMENT=癲ev"; 公共静态最终字符串SPRING_PROFILE_PRODUCTION=皃rod"; 私人常数(){ } }使用弹簧引导实现档案动态切换