spring boot实现profiles动态切换的示例

网友投稿 277 2022-11-18


spring boot实现profiles动态切换的示例

具体做法:

1、首先在pom中添加profiles:

dev

true

dev

org.springframework.boot

spring-boot-devtools

true

org.springframework.boot

spring-boot-starter-undertow

prod

org.springframework.boot

spring-boot-starter-undertow

prod

dev指开发模式,prod指生产模式,如需其他模式,只需要添加profile即可.

2、在pom.xml的build中添加plugin:

org.apache.maven.plugins

maven-resources-plugin

${maven-resources-plugin.version}

default-resources

validate

copy-resources

target/classes

false

#

src/main/resources/

true

**/*.xml

**/*.yml

src/main/resources/

false

**/*.xml

**/*.yml

该配置用来在打包的时候修改配置文件。

3、编写DefaultProfileUtil工具类来添加默认启动配置文件:

import org.springframework.boot.SpringApplication;

import org.springframework.core.env.Environment;

import java.util.HashMap;

import java.util.Map;

/**

* Utility class to load a Spring profile to be used as default

* when there is no spring.profiles.active set in the environment or as command line argument.

* If the value is not available in application.yml then dev profile will be used as default.

*/

public final class DefaultProfileUtil {

private static final String SPRING_PROFILE_DEFAULT = "spring.profiles.default";

private DefaultProfileUtil(){

}

/**

* Set a default to use when no profile is configured.

*

* @param app the spring application

*/

public static void addDefaultProfile(SpringApplication app) {

Map defProperties = new HashMap<>();

/*

* The default profile to use when no other profiles are defined

* This cannot be set in the application.yml file.

* See https://github.com/spring-HjacDsUlprojects/spring-boot/issues/1219

*/

defProperties.put(SPRING_PROFILE_DEFAULT, Constants.SPRING_PROFILE_DEVELOPMENT);

app.setDefaultProperties(defProperties);

System.out.println(app);

}

/**

* Get the profiles that are applied else get default profiles.

*/

public static String[] getActiveProfiles(Environment env) {

String[] profiles = env.getActiveProfiles();

if (profiles.length == 0) {

return env.getDefaultProfiles();

}

return profiles;

}

}

public class Constants {

public static final String SPRING_PROFILE_DEVELOPMENT = "dev";

public static final String SPRING_PROFILE_PRODUCTION = "prod";

private Constants() {

}

}

4、修改application.yml配置文件,添加(采用application.properties文件):

spring:

profiles:

active: #spring.profiles.active#

maven的构建的时候会替换#spring.profiles.active#

5、修改项目的启动类:

@SpringBootApplication

public class Demo1Application {

private static final Logger log = LoggerFactory.getLogger(Demo1Application.class);

http:// public static void main(String[] args) {

SpringApplication app = new SpringApplication(Demo1Application.class);

DefaultProfileUtil.addDefaultProfile(app);

Environment env = app.run(args).getEnvironment();

log.info("\n----------------------------------------------------------\n\t" +

"Application '{}' is running! Access URLs:\n\t" +

"Local: \t\thttp://localhost:{}\n\t" +

"----------------------------------------------------------",

env.getProperty("spring.application.name"),

env.getProperty("server.port"));

}

}

以上修改完成之后,在启动的时候会显示:The following profiles are active: dev 默认dev模式切换成功。

6、构建项目:

采用mvn clean package -Pprod命令构建,最后的配置文件会被改成:

以上就是spring boot实现profiles动态切换的示例的详细内容,更多关于spring boot实现profiles动态切换的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:分析HashMap 的 JDK 源码
下一篇:MyBatis还是JPA?终于有答案了
相关文章

 发表评论

暂时没有评论,来抢沙发吧~