av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

spring boot實現profiles動態切換的示例

瀏覽:86日期:2023-08-09 09:33:09

具體做法:

1、首先在pom中添加profiles:

<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <spring.profiles.active>dev</spring.profiles.active> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> </dependencies> </profile> <profile> <id>prod</id> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> </dependencies> <properties> <spring.profiles.active>prod</spring.profiles.active> </properties> </profile></profiles>

dev指開發模式,prod指生產模式,如需其他模式,只需要添加profile即可.

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

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>${maven-resources-plugin.version}</version> <executions> <execution> <id>default-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>target/classes</outputDirectory> <useDefaultDelimiters>false</useDefaultDelimiters> <delimiters><delimiter>#</delimiter> </delimiters> <resources><resource> <directory>src/main/resources/</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> <include>**/*.yml</include> </includes></resource><resource> <directory>src/main/resources/</directory> <filtering>false</filtering> <excludes> <exclude>**/*.xml</exclude> <exclude>**/*.yml</exclude> </excludes></resource> </resources> </configuration> </execution> </executions></plugin>

該配置用來在打包的時候修改配置文件。

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 <code>spring.profiles.active</code> set in the environment or as command line argument. * If the value is not available in <code>application.yml</code> then <code>dev</code> 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<String, Object> defProperties = new HashMap<>(); /* * The default profile to use when no other profiles are defined * This cannot be set in the <code>application.yml</code> file. * See https://github.com/spring-projects/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、修改項目的啟動類:

@SpringBootApplicationpublic class Demo1Application { private static final Logger log = LoggerFactory.getLogger(Demo1Application.class); public static void main(String[] args) { SpringApplication app = new SpringApplication(Demo1Application.class); DefaultProfileUtil.addDefaultProfile(app); Environment env = app.run(args).getEnvironment(); log.info('n----------------------------------------------------------nt' + 'Application ’{}’ is running! Access URLs:nt' + 'Local: tthttp://localhost:{}nt' + '----------------------------------------------------------', env.getProperty('spring.application.name'), env.getProperty('server.port')); }}

以上修改完成之后,在啟動的時候會顯示:The following profiles are active: dev 默認dev模式切換成功。

spring boot實現profiles動態切換的示例

6、構建項目:

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

spring boot實現profiles動態切換的示例

以上就是spring boot實現profiles動態切換的示例的詳細內容,更多關于spring boot實現profiles動態切換的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 日韩一级不卡 | 欧美精品成人一区二区三区四区 | 欧美精品1区2区 | 亚洲高清在线 | 成人一区精品 | 欧美日韩专区 | 国产一区二区欧美 | 日本黄色的视频 | 成人在线不卡 | 午夜欧美a级理论片915影院 | 精品视频一区二区三区在线观看 | 在线免费观看日本视频 | 日韩一区二区在线播放 | 亚洲视频二区 | 最近中文字幕免费 | 成人1区2区 | 成人在线中文字幕 | 久久小视频 | 365夜爽爽欧美性午夜免费视频 | 九九色九九| 日韩在线免费视频 | 中文字幕日韩一区 | 99精品一区二区三区 | 亚洲欧洲精品在线 | 日韩国产一区二区三区 | 黄色免费网 | 精品国产一区二区三区久久久久久 | 完全免费在线视频 | 色综合天天综合网国产成人网 | 二区在线视频 | 成人免费一区二区三区视频网站 | 国产视频中文字幕 | 免费v片在线观看 | 国产激情一区二区三区 | 久久久999国产精品 中文字幕在线精品 | 一区二区三区国产精品 | 久久精品视频在线观看 | 中文字幕在线一区 | 国产一二三区电影 | 91社区在线观看播放 | 能看的av|