【SpringBoot】SpringBoot之入門配置檔案
阿新 • • 發佈:2019-01-25
介紹:example02
相信很多人選擇Spring Boot主要是考慮到它既能兼顧Spring的強大功能,還能實現快速開發的便捷。我們在Spring Boot使用過程中,最直觀的感受就是沒有了原來自己整合Spring應用時繁多的XML配置內容,替代它的是在pom.xml中引入模組化的Starter POMs,其中各個模組都有自己的預設配置,所以如果不是特殊應用場景,就只需要在application.properties中完成一些屬性配置就能開啟各模組的應用。
配置檔案application.properties的使用,主要用來配置資料庫連線、日誌相關配置等。除了這些配置內容之外,本文將具體介紹一些在application.properties配置中的其他特性和使用方法。
我們在使用Spring Boot的時候,通常也需要定義一些自己使用的屬性,我們可以如下方式直接定義:
application.properties
test.springboot.name=學習
test.springboot.context=我
#引數間引用
test.springboot.desc=${test.springboot.context}愛${test.springboot.name}
#隨機數
##隨機字串
test.springboot.value=${random.value}
##隨機int
test.springboot.number=${random.int }
##隨機long
test.springboot.bignumber=${random.long}
##10以內的隨機數
test.springboot.test1=${random.int(10)}
##10-20的隨機數
test.springboot.test2=${random.int[10,20]}
#多環境配置檔案啟用屬性
spring.profiles.active=dev
# 服務埠
server.port=9090
##設定utf-8編碼集
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http .encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8
test.springboot.desc引數引用了上文中定義的name和context屬性,最後該屬性的值就是:我愛學習
引用值:通過@Value(“${屬性名}”)註解來載入對應的配置屬性,具體如下:
建立實體類:TestProperties
package com.lyd.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class TestProperties {
@Value("${test.springboot.name}")
private String name;
@Value("${test.springboot.context}")
private String context;
@Value("${test.springboot.desc}")
private String desc;
//測試隨機數配置
@Value("${test.springboot.value}")
private String value;
//get/set方法...
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Application.java
package com.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author : xiaolin
* @Description: Created by Administrator on 2017/11/22.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
ApplicationTest.java單元測試類
package com.lyd;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.validator.internal.util.privilegedactions.GetConstraintValidatorList;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.lyd.entity.TestProperties;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTest {
private static final Log log = LogFactory.getLog(ApplicationTest.class);
@Autowired
private TestProperties testProperties;
@Test
public void getHello(){
Assert.assertEquals(testProperties.getName(), "學習");
Assert.assertEquals(testProperties.getDesc(), "我愛學習");
System.out.println(testProperties.getName());
log.info("隨機字串測試:"+testProperties.getValue());
}
}
pom檔案
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lyd</groupId>
<artifactId>springboot-lyd</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>example02</artifactId>
</project>
專案目錄
執行getHello方法,可以讀到application.properties中的資料: