国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

springboot 多環境配置教程

2024-07-13 10:17:35
字體:
來源:轉載
供稿:網友

在上一課中我們通過idea工具沒有做任何配置就構建了一個springboot項目,并且已經成功啟動了,但我們都很清楚這些都遠遠不能達到我們實際項目的需求,比如我們要引入我們自己的redis配置、mysql配置等,應該如何處理呢?在spring mvc中我們都是通過spring.xml相關文件配置,在springboot中這些都已經不存在了,我們應該怎樣配置呢?別急,馬上為大家揭曉謎底,跟著我一起來吧!

NO1.我們在做項目的時候是不是都會區分很多環境呢?比如開發環境、測試環境、生產環境等,那么第一步我將先帶大家配置好各個環境;

1.首先打開我們項目的pom.xml文件加入以下內容:

<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>

這一段相信大家都很熟悉了吧,我就不多做解釋了(有疑問的童鞋可以私信我哦);

2.然后打開application.properties文件,并在其中加入以下內容:

#表示激活的配置文件(dev|prod)
spring.profiles.active=@filter-resource-name@

整個項目變成了如下結構:

spring,boot,環境配置,springboot

至此我們的springboot多環境配置已經完成;

3.設置日志級別

#log levellogging.level.root=debug

4.設置自定義端口以及實例名

#端口server.port=8888#實例名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

至此,我們項目的基本環境配置已經搭建好,通過maven clean install以下選擇dev|test|prod打入你指定的配置,然后run application運行,如果通過localhost:8888可以訪問說明你的配置worked了;但是這還遠遠不夠,我們項目開發總得操作數據庫吧,哈哈 是的,接下來讓我們進入springboot + mysql + mybatis的世界吧!

總結

以上所述是小編給大家介紹的springboot 多環境配置教程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泽库县| 青浦区| 九台市| 乌什县| 油尖旺区| 泊头市| 常宁市| 南开区| 白城市| 正镶白旗| 微博| 全州县| 多伦县| 常熟市| 横峰县| 礼泉县| 塔城市| 扎赉特旗| 会昌县| 新兴县| 静安区| 迁安市| 泸水县| 肇东市| 呼图壁县| 久治县| 丹寨县| 抚州市| 邵阳县| 偃师市| 固原市| 兰考县| 焦作市| 三门峡市| 莱阳市| 江永县| 溆浦县| 三江| 沅江市| 章丘市| 当涂县|