最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot配置logback的步驟

 更新時(shí)間:2020年09月09日 11:22:26   作者:崔笑顏  
這篇文章主要介紹了SpringBoot配置logback的步驟,幫助大家更好的理解和使用SpringBoot框架,感興趣的朋友可以了解下

配置日志文件

spring boot 默認(rèn)會(huì)加載 classpath:logback-spring.xml 或者 classpath:logback-spring.groovy。

如需要自定義文件名稱,在 application.properties 中配置 logging.config 選項(xiàng)即可。

在 src/main/resources 下創(chuàng)建 logback-spring.xml 文件,內(nèi)容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <!-- 文件輸出格式 -->
  <property name="PATTERN" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" />
  <!-- test文件路徑 -->
  <property name="TEST_FILE_PATH" value="d:/test.log" />
  <!-- pro文件路徑 -->
  <property name="PRO_FILE_PATH" value="/opt/test/log" />

  <!-- 開發(fā)環(huán)境 -->
  <springProfile name="dev">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
        <pattern>${PATTERN}</pattern>
      </encoder>
    </appender>
    <logger name="com.light.springboot" level="debug" />
    <root level="info">
      <appender-ref ref="CONSOLE" />
    </root>
  </springProfile>

  <!-- 測(cè)試環(huán)境 -->
  <springProfile name="test">
    <!-- 每天產(chǎn)生一個(gè)文件 -->
    <appender name="TEST-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <!-- 文件路徑 -->
      <file>${TEST_FILE_PATH}</file>
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- 文件名稱 -->
        <fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
        <!-- 文件最大保存歷史數(shù)量 -->
        <MaxHistory>100</MaxHistory>
      </rollingPolicy>
      <layout class="ch.qos.logback.classic.PatternLayout">
        <pattern>${PATTERN}</pattern>
      </layout>
    </appender>
    <logger name="com.light.springboot" level="debug" />
    <root level="info">
      <appender-ref ref="TEST-FILE" />
    </root>
  </springProfile>

  <!-- 生產(chǎn)環(huán)境 -->
  <springProfile name="prod">
    <appender name="PROD_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <file>${PRO_FILE_PATH}</file>
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern>
        <MaxHistory>100</MaxHistory>
      </rollingPolicy>
      <layout class="ch.qos.logback.classic.PatternLayout">
        <pattern>${PATTERN}</pattern>
      </layout>
    </appender>
    <root level="warn">
      <appender-ref ref="PROD_FILE" />
    </root>
  </springProfile>
</configuration>

其中,springProfile 標(biāo)簽的 name 屬性對(duì)應(yīng) application.properties 中的 spring.profiles.active 的配置。

即 spring.profiles.active 的值可以看作是日志配置文件中對(duì)應(yīng)的 springProfile 是否生效的開關(guān)。

注解介紹

下面列出 Spring Boot 開發(fā)中常用的注解:

@Configuration         # 作用于類上,相當(dāng)于一個(gè) xml 配置文件
@Bean             # 作用于方法上,相當(dāng)于 xml 配置中的 <bean>
@SpringBootApplication     # Spring Boot的核心注解,是一個(gè)組合注解,用于啟動(dòng)類上
@EnableAutoConfiguration    # 啟用自動(dòng)配置,允許加載第三方 Jar 包的配置
@ComponentScan         # 默認(rèn)掃描 @SpringBootApplication 所在類的同級(jí)目錄以及它的子目錄
@PropertySource        # 加載 properties 文件
@Value             # 將配置文件的屬性注入到 Bean 中特定的成員變量
@EnableConfigurationProperties # 開啟一個(gè)特性,讓配置文件的屬性可以注入到 Bean 中,與 @ConfigurationProperties 結(jié)合使用
@ConfigurationProperties    # 關(guān)聯(lián)配置文件中的屬性到 Bean 中
@Import            # 加載指定 Class 文件,其生命周期被 Spring 管理
@ImportResource        # 加載 xml 文件

讀取配置文件

屬性裝配

有兩種方式:使用 @Value 注解和 Environment 對(duì)象。 在 application.properties 中添加:

ds.userName=root
ds.password=tiger
ds.url=jdbc:mysql://localhost:3306/test
ds.driverClassName=com.mysql.jdbc.Driver

以上是自定義的配置。
創(chuàng)建一個(gè)配置類,如下:
@Configuration
public class WebConfig {

  @Value("${ds.userName}")
  private String userName;

  @Autowired
  private Environment environment;

  public void show() {
    System.out.println("ds.userName:" + this.userName);
    System.out.println("ds.password:" + this.environment.getProperty("ds.password"));
  }
}

通過 @Value 獲取 config.userName 配置;通過 environment 獲取 config.password 配置。

測(cè)試:
@SpringBootApplication
public class SpringbootApplication {

  public static void main(String[] args) {
    ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
    context.getBean(WebConfig.class).show();
  }
}

打印結(jié)果:
userName:root
password:tiger

對(duì)象裝配

創(chuàng)建一個(gè)封裝類: 省略 get set

@Component
@ConfigurationProperties(prefix="ds")
public class DataSourceProperties {

  private String url;

  private String driverClassName;

  private String userName;

  private String password;


  public void show() {
    System.out.println("ds.url=" + this.url);
    System.out.println("ds.driverClassName=" + this.driverClassName);
    System.out.println("ds.userName=" + this.userName);
    System.out.println("ds.password=" +this.password);
  }

}
測(cè)試:
@SpringBootApplication
public class SpringbootApplication {

  public static void main(String[] args) {
    ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
    context.getBean(DataSourceProperties.class).show();
  }
}

打印結(jié)果
ds.url=jdbc:mysql://localhost:3306/test
ds.driverClassName=com.mysql.jdbc.Driver
ds.userName=root
ds.password=tiger

打包運(yùn)行

打包的形式有兩種:jar 和 war。

jar

默認(rèn)情況下,通過 maven 執(zhí)行 package 命令后,會(huì)生成 jar 包,且該 jar 包會(huì)內(nèi)置了 tomcat 容器,因此我們可以通過 java -jar 就可以運(yùn)行項(xiàng)目

war

讓 SpringbootApplication 類繼承 SpringBootServletInitializer 并重寫 configure 方法,如下:

@SpringBootApplication
public class SpringbootApplication extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(SpringbootApplication.class);
  }

  public static void main(String[] args) {
    SpringApplication.run(SpringbootApplication.class, args);
  }
}

修改 pom.xml 文件,將 jar 改成 war,如下:

<packaging>war</packaging>
移除內(nèi)置 tomcat:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<!-- Servlet API -->
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
	<version>3.1.0</version>
	<scope>provided</scope>
</dependency>

以上就是SpringBoot配置logback的步驟的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot配置logback的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 源碼分析ConcurrentHashMap如何保證線程安全

    源碼分析ConcurrentHashMap如何保證線程安全

    這篇文章將結(jié)合底層源碼為大家詳細(xì)介紹一下ConcurrentHashMap是如何保證線程安全的,文中是示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-06-06
  • Spring中的Schedule動(dòng)態(tài)添加修改定時(shí)任務(wù)詳解

    Spring中的Schedule動(dòng)態(tài)添加修改定時(shí)任務(wù)詳解

    這篇文章主要介紹了Spring中的Schedule動(dòng)態(tài)添加修改定時(shí)任務(wù)詳解,可能有人會(huì)問,為啥不用Quartz,Quartz自然是非常方便強(qiáng)大的,但不是本篇要講的內(nèi)容,本篇就偏要使用SpringSchedule來實(shí)現(xiàn)動(dòng)態(tài)的cron表達(dá)式任務(wù),需要的朋友可以參考下
    2023-11-11
  • Java之PreparedStatement的使用詳解

    Java之PreparedStatement的使用詳解

    這篇文章主要介紹了Java之PreparedStatement的使用詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • JavaWeb文件上傳開發(fā)實(shí)例

    JavaWeb文件上傳開發(fā)實(shí)例

    這篇文章主要為大家詳細(xì)介紹了JavaWeb文件上傳開發(fā)實(shí)例,如何進(jìn)行文件上傳操作,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 詳解java中static關(guān)鍵詞的作用

    詳解java中static關(guān)鍵詞的作用

    這篇文章主要介紹了java中static關(guān)鍵詞的作用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • SpringCloud Gateway 路由配置定位原理分析

    SpringCloud Gateway 路由配置定位原理分析

    本節(jié)主要了解系統(tǒng)中的謂詞與配置的路由信息是如何進(jìn)行初始化關(guān)聯(lián)生成路由對(duì)象的。每個(gè)謂詞工廠中的Config對(duì)象又是如何被解析配置的
    2021-07-07
  • Java ArrayList中存放引用數(shù)據(jù)類型的方式

    Java ArrayList中存放引用數(shù)據(jù)類型的方式

    這篇文章主要介紹了Java ArrayList中存放引用數(shù)據(jù)類型的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 在SpringBoot下讀取自定義properties配置文件的方法

    在SpringBoot下讀取自定義properties配置文件的方法

    這篇文章主要介紹了在SpringBoot下讀取自定義properties配置文件的方法,文中涉及到了Spring-boot中讀取config配置文件的兩種方式,需要的朋友可以參考下
    2017-12-12
  • springboot jar包外置配置文件的解決方法

    springboot jar包外置配置文件的解決方法

    這篇文章主要給大家介紹了關(guān)于spring boot jar包外置配置文件的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 一招教你優(yōu)化Java代碼中大量的if/else

    一招教你優(yōu)化Java代碼中大量的if/else

    當(dāng)代碼已經(jīng)復(fù)雜到難以維護(hù)的程度之后,只能狠下心重構(gòu)優(yōu)化。那,有什么方案可以優(yōu)雅的優(yōu)化掉這些多余的if/else?本文就來和大家詳細(xì)聊聊
    2023-03-03

最新評(píng)論

盖州市| 衡东县| 格尔木市| 东乡族自治县| 新泰市| 康定县| 霸州市| 榆树市| 徐州市| 吉林省| 盈江县| 丽江市| 余庆县| 遂平县| 和平县| 德兴市| 延寿县| 平山县| 武宁县| 浦东新区| 汉沽区| 沙湾县| 汉源县| 古田县| 昌都县| 商河县| 关岭| 水富县| 灵寿县| 句容市| 通渭县| 东台市| 沂南县| 民勤县| 永新县| 江城| 伊金霍洛旗| 集贤县| 都匀市| 左权县| 从化市|