springboot 多環(huán)境配置教程
在上一課中我們通過(guò)idea工具沒(méi)有做任何配置就構(gòu)建了一個(gè)springboot項(xiàng)目,并且已經(jīng)成功啟動(dòng)了,但我們都很清楚這些都遠(yuǎn)遠(yuǎn)不能達(dá)到我們實(shí)際項(xiàng)目的需求,比如我們要引入我們自己的redis配置、mysql配置等,應(yīng)該如何處理呢?在spring mvc中我們都是通過(guò)spring.xml相關(guān)文件配置,在springboot中這些都已經(jīng)不存在了,我們應(yīng)該怎樣配置呢?別急,馬上為大家揭曉謎底,跟著我一起來(lái)吧!
NO1.我們?cè)谧鲰?xiàng)目的時(shí)候是不是都會(huì)區(qū)分很多環(huán)境呢?比如開(kāi)發(fā)環(huán)境、測(cè)試環(huán)境、生產(chǎn)環(huán)境等,那么第一步我將先帶大家配置好各個(gè)環(huán)境;
1.首先打開(kāi)我們項(xiàng)目的pom.xml文件加入以下內(nèi)容:
<build> <finalName>${project.artifactId}-${project.version}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf8</encoding> </configuration> </plugin> </plugins> <filters> <filter>src/main/resources/application-${filter-resource-name}.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>filters/*</exclude> <exclude>filters/*</exclude> <exclude>application-dev.properties</exclude> <exclude>application-test.properties</exclude> <exclude>application-alpha.properties</exclude> <exclude>application-prod.properties</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application-${filter-resource-name}.properties</include> </includes> </resource> </resources> </build> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <filter-resource-name>dev</filter-resource-name> </properties> </profile> <profile> <id>test</id> <properties> <filter-resource-name>test</filter-resource-name> </properties> </profile> <profile> <id>alpha</id> <properties> <filter-resource-name>alpha</filter-resource-name> </properties> </profile> <profile> <id>prod</id> <properties> <filter-resource-name>prod</filter-resource-name> </properties> </profile> </profiles>
這一段相信大家都很熟悉了吧,我就不多做解釋了(有疑問(wèn)的童鞋可以私信我哦);
2.然后打開(kāi)application.properties文件,并在其中加入以下內(nèi)容:
#表示激活的配置文件(dev|prod)
spring.profiles.active=@filter-resource-name@
整個(gè)項(xiàng)目變成了如下結(jié)構(gòu):
至此我們的springboot多環(huán)境配置已經(jīng)完成;
3.設(shè)置日志級(jí)別
#log level logging.level.root=debug
4.設(shè)置自定義端口以及實(shí)例名
#端口 server.port=8888 #實(shí)例名 spring.application.name=demo-springboot
5.logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <appender name="demo" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>demo/demo.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 按天回滾 daily --> <fileNamePattern>demo/demo.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- 日志最大的歷史 10天 --> <maxHistory>10</maxHistory> </rollingPolicy> <encoder charset="UTF-8"> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> </appender> <logger name="com.example.demo" level="INFO" additivity="false"> <appender-ref ref="demo"/> </logger> <logger name="com.example.demo.dao" level="DEBUG" /> <logger name="com.example.demo.service" level="INFO" /> <logger name="druid.sql.Statement" level="DEBUG" /> <logger name="druid.sql.ResultSet" level="DEBUG" /> <logger name="org.apache" level="INFO" /> <logger name="org.mybatis.spring" level="ERROR" /> <logger name="org.springframework" level="INFO"></logger> <logger name="springfox" level="ERROR"></logger> <root level="INFO"> <appender-ref ref="demo" /> </root> </configuration
至此,我們項(xiàng)目的基本環(huán)境配置已經(jīng)搭建好,通過(guò)maven clean install以下選擇dev|test|prod打入你指定的配置,然后run application運(yùn)行,如果通過(guò)localhost:8888可以訪問(wèn)說(shuō)明你的配置worked了;但是這還遠(yuǎn)遠(yuǎn)不夠,我們項(xiàng)目開(kāi)發(fā)總得操作數(shù)據(jù)庫(kù)吧,哈哈 是的,接下來(lái)讓我們進(jìn)入springboot + mysql + mybatis的世界吧!
總結(jié)
以上所述是小編給大家介紹的springboot 多環(huán)境配置教程,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Nacos入門(mén)過(guò)程的坑--獲取不到配置的值問(wèn)題
這篇文章主要介紹了Nacos入門(mén)過(guò)程的坑--獲取不到配置的值問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01java8中NIO緩沖區(qū)(Buffer)的數(shù)據(jù)存儲(chǔ)詳解
在本篇文章中小編給大家分享了關(guān)于java8中NIO緩沖區(qū)(Buffer)的數(shù)據(jù)存儲(chǔ)的相關(guān)知識(shí)點(diǎn),需要的朋友們參考下。2019-04-04SpringBoot發(fā)送html郵箱驗(yàn)證碼功能
這篇文章主要介紹了SpringBoot發(fā)送html郵箱驗(yàn)證碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12SpringBoot項(xiàng)目解決跨域的四種方案分享
在用SpringBoot開(kāi)發(fā)后端服務(wù)時(shí),我們一般是提供接口給前端使用,但前端通過(guò)瀏覽器調(diào)我們接口時(shí),瀏覽器會(huì)有個(gè)同源策略的限制,即協(xié)議,域名,端口任一不一樣時(shí)都會(huì)導(dǎo)致跨域,這篇文章主要介紹跨域的幾種常用解決方案,希望對(duì)大家有所幫助2023-05-05詳解SpringCloud eureka服務(wù)狀態(tài)監(jiān)聽(tīng)
這篇文章主要介紹了詳解SpringCloud eureka服務(wù)狀態(tài)監(jiān)聽(tīng),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07淺談一個(gè)基礎(chǔ)的SpringBoot項(xiàng)目該包含哪些
這篇文章主要介紹了淺談一個(gè)基礎(chǔ)的SpringBoot項(xiàng)目該包含哪些,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10SpringBoot實(shí)現(xiàn)支付寶沙箱支付的完整步驟
沙箱支付是一種用于模擬真實(shí)支付環(huán)境的測(cè)試工具,它提供了一個(gè)安全的測(cè)試環(huán)境,供開(kāi)發(fā)者在不影響真實(shí)交易的情況下進(jìn)行支付功能的開(kāi)發(fā)和測(cè)試,這篇文章給大家介紹了SpringBoot實(shí)現(xiàn)支付寶沙箱支付的完整步驟,需要的朋友可以參考下2024-04-04詳解Spring依賴注入:@Autowired,@Resource和@Inject區(qū)別與實(shí)現(xiàn)原理
這篇文章主要介紹了詳解Spring依賴注入:@Autowired,@Resource和@Inject區(qū)別與實(shí)現(xiàn)原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Java實(shí)現(xiàn)解析ini文件對(duì)應(yīng)到JavaBean中
ini 文件是Initialization File的縮寫(xiě),即初始化文件,是windows的系統(tǒng)配置文件所采用的存儲(chǔ)格式。這篇文章主要介紹了通過(guò)Java實(shí)現(xiàn)解析ini文件對(duì)應(yīng)到JavaBean中,需要的可以參考一下2022-01-01