一、配置文件配置
直接配置
在src/main/resources下添加配置文件application.properties
例如修改端口號
#端口號 server.port=8089
分環境配置
在src/main/resources下添加,application-pro.properties,application-dev.properties和application.properties三個文件
application.properties
spring.profiles.active=dev
application-pro.properties
#端口號 server.port=80 #自定義端口號讀取 my.name=pzr.dev
application-dev.properties
#端口號 server.port=8089 #自定義端口號讀取 my.name=pzr.pro
當application.propertie設置spring.profiles.active=dev時,則說明是指定使用application-dev.properties文件進行配置
二、配置文件參數讀取
2.1、注解方式讀取
1、@PropertySource配置文件路徑設置,在類上添加注解,如果在默認路徑下可以不添加該注解。
需要用@PropertySource的有:
- 例如非application.properties,classpath:config/my.properties指的是src/main/resources目錄下config目錄下的my.properties文件,
- 例如有
多配置文件引用,若取兩個配置文件中有相同屬性名的值,則取值為最后一個配置文件中的值 在application.properties中的文件,直接使用@Value讀取即可,applicarion的讀取優先級最高
@PropertySource({"classpath:config/my.properties","classpath:config/config.properties"})
public class TestController
2、@Value屬性名,在屬性名上添加該注解
@Value("${my.name}")
private String myName;
示例1:使用@Value讀取application.properties里的配置內容
配置文件application.properties
spring.application.name=springbootdemo server.port=8080 mail.username=application-duan mail.password=application-duan123456
啟動類
package com.dxz.property5;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class TestProperty5 {
public static void main(String[] args) {
//SpringApplication.run(TestProperty1.class, args);
new SpringApplicationBuilder(TestProperty5.class).web(true).run(args);
}
}
測試類:
package com.dxz.property5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/task")
//@PropertySource("classpath:mail.properties")
public class TaskController {
@Value("${mail.username}")
private String userName;
@Value("${mail.password}")
private String password;
@RequestMapping(value = { "/", "" })
public String hellTask() {
System.out.println("userName:" + userName);
System.out.println("password:" + password);
return "hello task !!";
}
}
結果:
userName:application-duan password:application-duan123456
示例2:使用@Value+@PropertySource讀取其它配置文件(多個)內容
讀取mail.properties配置
package com.dxz.property5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/task")
@PropertySource("classpath:mail.properties")
public class TaskController {
@Value("${mail.smtp.auth}")
private String userName;
@Value("${mail.from}")
private String password;
@RequestMapping(value = { "/", "" })
public String hellTask() {
System.out.println("userName:" + userName);
System.out.println("password:" + password);
return "hello task !!";
}
}
結果:
userName:false password:me@localhost

