springboot快速入門(二)——項目屬性配置(日志詳解)


一、概述

  application.properties就是springboot的屬性配置文件

  在使用spring boot過程中,可以發現項目中只需要極少的配置就能完成相應的功能,這歸功於spring boot中的模塊化配置,在pom.xml中依賴的每個Starter都有默認配置,而這些默認配置足以滿足正常的功能開發。

  除此之外,還有application.yml形式的配置文件;對比如下;

server.port=8080  
server.session-timeout=30  
server.context-path=  
server.tomcat.max-threads=0  
server.tomcat.uri-encoding=UTF-8  
  
spring.datasource.url = jdbc:mysql://localhost:3306/spring  
spring.datasource.username = root  
spring.datasource.password = root  
spring.datasource.driverClassName = com.mysql.jdbc.Driver  
# Specify the DBMS  
spring.jpa.database = MYSQL  
# Show or not log for each sql query  
spring.jpa.show-sql = true  
# Hibernate ddl auto (create, create-drop, update)  
spring.jpa.hibernate.ddl-auto = update  
# Naming strategy  
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy  
  
# stripped before adding them to the entity manager)  
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect  
application.properties
server:  
  port: 8080  
  session-timeout: 30  
  tomcat.max-threads: 0  
  tomcat.uri-encoding: UTF-8  
  
spring:  
  datasource:  
    url : jdbc:mysql://localhost:3306/springboot  
    username : root  
    password : root  
    driverClassName : com.mysql.jdbc.Driver  
  jpa:  
    database : MYSQL  
    show-sql : true  
    hibernate:  
      ddl-auto : update  
      naming-strategy : org.hibernate.cfg.ImprovedNamingStrategy  
    properties:  
      hibernate:  
application.yml

  默認生成的為application.properties

  # 補充springboot日志詳解

二、修改默認配置

  spring-boot自帶了很多默認配置,所有默認配置可以參考官方文檔:點擊查看所有默認配置

  示例:修改端口和context-path:

server.port=8081 server.context-path=/demo

  // 新版的IDEA寫此配置文件也會給出對應默認配置名稱的提示,贊!

  

 

  當然,通過jar方式運行也可以使用--進行參數設置,這是專門用於對屬性文件進行配置的:

java -jar xxx.jar --server.port=8888

  當然,properties寫起來比較費事,我們推薦的是yml(壓ml)形式的配置:

    這里必須提醒,yml配置文件分割處必須有空格:當然IDEA也會幫我們檢查yml語法,正確的配置是會高亮的,yml文件本身也會有特殊圖標顯示的

    

    這里我們就把默認的properties刪除了,使用更加簡潔的yml配置:

server: port: 8082 context-path: /demo

    如果已經是properties,可以通過在線工具轉換http://www.toyaml.com/

    效果:

    

 

 三、自定義屬性配置

  1.在yml文件中定義:

salary: 10010 name: jiangbei

  2.使用@Value注解讀取值:

    使用@Value的類如果被其他類作為對象引用,必須要使用注入的方式,而不能new

package com.example.demo; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * 測試demo的controller * * @author zcc ON 2018/2/8 **/ @RestController public class HelloController { @Value("${name}") private String name; @Value("${salary}") private Integer salary; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello() { return "name:" + name + " salary:" + salary; } }

  頁面效果:

  

  // 可以看到,配置文件到屬性的過程,會自動進行類型轉換

    讀取多級屬性:

name:
  first_name: jiangbei
  last_name: z
  @Value("${name.first_name}") private String fname; @Value("${name.last_name}") private String lname;

    屬性中引用其他屬性:類似shell變量

name: first_name: jiangbei last_name: z full_name: "full name: ${name.first_name} ${name.last_name}"

    使用bean進行屬性映射

    需要特別注意的是bean上面的兩個注解:@Componet和@ConfigurationProperties,通過prefix來匹配前綴(類似分組思想),當然也可以

通過 @ConfigurationProperties(prefix = "master.ds",locations = "classpath:application.properties")  的形式,指定配置文件

 

 

person: name: jiangbei age: 18 sex: M
package com.example.demo.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * 映射屬性配置的bean * * @author zcc ON 2018/2/8 **/ @ConfigurationProperties(prefix = "person") @Component public class PersonProp { private String name; private Integer age; private String sex; // getter setter未列出
}
@RestController public class HelloController { @Autowired private PersonProp person; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello() { return person.getName(); } }

    多配置文件切換:

      例如生產和開發的配置文件需要切換,那我們只要定義application-xxx.yml,然后在application.yml里面使用spring.profile.active切換即可!

    

    在application.yml中進行配置:

spring:
  profiles:
    active: dev

    這個時候再在application.yml文件里面寫配置的話,dev里面也是可以直接用到的!

   更多屬性特征與使用,參考: http://blog.didispace.com/springbootproperties/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM