屬性配置
spring.datasource.url: jdbc:mysql://127.0.0.1:3306/
spring.datasource.username: root
spring.datasource.password: 123456
spring.datasource.driver-class-name: com.mysql.jdbc
刪除girl項目中resource目錄下static和templates目錄。
application.properties是項目的配置文件。
簡單配置一下端口和context-path:
server.port=8081
server.servlet.context-path=/girl
啓動項目,訪問 http://127.0.0.1:8080/hello 不起作用,因爲我們改了端口號,並且給url加上類前綴。新的訪問地址:http://127.0.0.1:8081/girl/hello
配置文件類型
默認項目是.properties文件,但推薦使用.yml文件格式。
在.yml文件中,上述端口和路徑的配置可寫成:
server:
port: 8081
servlet:
context-path: /girl
注意:yml語法冒號後面必須加上空格。否則是錯誤的。idea對yml語法了很好對支持。
配置文件只需要一個,我們這裏把.properties文件刪除,留下.yml配置文件。至此,項目的目錄結構爲下圖所示。
舉個例子
通過一個小例子來感受配置文件的使用。
假如6個女生分別size爲A、B、C、D、E、F
如果目的是找出size大於B的女生,那麼爲們在配置文件中把size配成B。
application.yml文件內容改成:
server:
port: 8080
cupSize: B
在HelloController裏使用 @Value 來寫。代碼如下所示。
package com.fiona;
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;
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say() {
return cupSize;
}
}
瀏覽器訪問 127.0.0.1:8080/hello
女生還有年齡屬性,我們把配置文件加上age屬性。
age: 18
和cupSize類似,我們在HelloController加上age屬性:
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@Value("${age}")
private Integer age;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say() {
return cupSize + age;
}
}
我們可以看到age是Integer類型的,配置文件中不需要判斷類型。只需要在注入進來的地方設置屬性類型就可以了。
啓動項目,在瀏覽器中返回結果 B18
如何在配置裏再使用配置呢?
application.yml中改成:
server:
port: 8080
cupSize: B
age: 18
content: "cupSize: ${cupSize}, age: ${age}"
HelloController中改成:
package com.fiona;
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;
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@Value("${age}")
private Integer age;
@Value("${content}")
private String content;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say() {
return content;
}
}
瀏覽器刷新一下
cupSize: B, age: 18
配置寫到類裏面
application.yml內容修改成:
server:
port: 8080
girl:
cupSize: B
age: 18
在 com.fiona包中新建一個GirlProperties類,使用@ConfigurationProperties(prefix = “girl”) 獲取前綴是girl的配置。
此時出現問題 Spring Boot Configuration Annotation Processor not found in classpath
在pom.xml中加上maven包依賴
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional> true </optional>
</dependency>
package com.fiona;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private String cupSize;
private Integer age;
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.fiona;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say() {
return girlProperties.getCupSize();
}
}
瀏覽器請求 http://127.0.0.1:8080/hello ,顯示結果B
不同環境使用不同配置
假設需求:開發環境 cupSize需求爲B,生成環境cupSize需求爲F
將application.yml拷貝兩份,分別命名爲application-dev.yml和application-prod.yml
application-dev.yml
server:
port: 8080
girl:
cupSize: B
age: 18
application-prod.yml
server:
port: 8081
girl:
cupSize: F
age: 18
application.yml
當前使用的是dev的配置
spring:
profiles:
active: dev
訪問 http://127.0.0.1:8080/hello 顯示B
spring:
profiles:
active: prod
訪問 http://127.0.0.1:8081/hello 顯示F
此時,我們可以通過第三種啓動方式啓動一種環境配置,再在idea裏啓動另一種,具體如下:
打開命令行
cd ~/Documents/dev/java/imooc/girl mvn install(項目路徑)
cd target
java -jar girl-0.0.1-SNAPSHOT.jar ----spring.profiles.active=prod
暫停停止服務按鍵盤control+c
再將idea中application.yml配置 active: dev 啓動項目
可以訪問:127.0.0.1:8081/hello 顯示F
可以訪問:127.0.0.1:8080/hello 顯示B
總結
屬性配置
@Value |
---|
@Component |
@ConfigurationProperties |