SpringBoot配置檔案註解
[email protected]
@PropertySource:載入指定的配置檔案properties
通過@ConfifigurationProperties載入全域性配置檔案中的值到JavaBean中,在開發時不會把所有的配置都儲存在全域性配置檔案中,可能會將不同的配置儲存在不同的配置檔案中,這時就需要@PropertySource註解為指定的javabean類載入指定的配置檔案。
例如:
javabean類:
package qing.bean;
import org.springframework.boot.context. properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "stu")
@PropertySource(value = {"classpath:student.properties"})
public class Student {
private int stuid;
private String stuname;
private double stuheight;
private String stuaddress;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public double getStuheight() {
return stuheight;
}
public void setStuheight(double stuheight) {
this.stuheight = stuheight;
}
public String getStuaddress() {
return stuaddress;
}
public void setStuaddress(String stuaddress) {
this.stuaddress = stuaddress;
}
}
student.properties 配置檔案:
stu.stuid=1001
stu.stuname=hello
stu.stuheight=123.2
stu.stuaddress=beijing
控制器類:
package qing.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qing.bean.Student;
@RestController
public class StudentController {
@Autowired
private Student student;
@RequestMapping("/student")
public String getStudent(){
int id=student.getStuid();
String name=student.getStuname();
double height=student.getStuheight();
String address=student.getStuaddress();
String info=id+name+height+address;
System.out.println(id+" "+name+" "+height+" "+address);
return info;
}
}
springboot主程式類:
package qing.springbootdemo4;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import qing.configuration.UserConfig;
@SpringBootApplication
@ComponentScan("qing")
public class Springbootdemo4Application {
public static void main(String[] args) {
SpringApplication.run(Springbootdemo4Application.class, args);
}
}
結果:
[email protected]
@ImportResource:匯入基於xml的Spring的配置檔案,使配置檔案裡面內容生效。
例如:
javabean實體類:
package qing.bean;
public class Person {
private int perid;
private String pername;
private int perage;
private boolean persex;
public int getPerid() {
return perid;
}
public void setPerid(int perid) {
this.perid = perid;
}
public String getPername() {
return pername;
}
public void setPername(String pername) {
this.pername = pername;
}
public int getPerage() {
return perage;
}
public void setPerage(int perage) {
this.perage = perage;
}
public boolean isPersex() {
return persex;
}
public void setPersex(boolean persex) {
this.persex = persex;
}
}
person.xml 配置檔案:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="qing.bean.Person">
<property name="perid" value="1002"/>
<property name="pername" value="xiaolitongxue"/>
<property name="perage" value="24"/>
<property name="persex" value="true"/>
</bean>
<bean name="/test" class="qing.controller.PersonController">
<property name="person" ref="person"/>
</bean>
</beans>
控制器類:
package qing.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import qing.bean.Person;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PersonController implements Controller {
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String info=person.getPerid()+"\t"+person.getPername()+"\t"+person.getPerage()+"\t"+person.isPersex();
System.out.println(info);
return null;
}
}
springboot主程式類:
package qing.springbootdemo4;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import qing.configuration.UserConfig;
@SpringBootApplication
@ComponentScan("qing")
@ImportResource(value = "person.xml")
public class Springbootdemo4Application {
public static void main(String[] args) {
SpringApplication.run(Springbootdemo4Application.class, args);
}
}
注意:Spring Boot裡面沒有Spring的配置檔案,自己編寫的配置檔案不能自動識別,讓Spring的配置檔案生效並載入進來需要使用@ImportResource註解標註在一個主類上。
結果:
[email protected]
@Bean新增在方法上,告訴SpringIOC容器建立javabean類物件,方法的返回值就是所要建立javabean類物件,法的名稱就是javabean類物件的名稱。
注意:@Bean必須出現在配置類中也就是@Configuration標註的java類。
例如:
業務方法類:
package qing.bean;
public class UserService {
public void getUser() {
System.out.println("User類測試方法!!");
}
}
配置類:
package qing.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import qing.bean.UserService;
@Configuration
public class UserConfig {
@Bean
public UserService getUserService() {
return new UserService();
}
}
控制器類:
package qing.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qing.bean.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/getUser")
public String getUser() {
userService.getUser();
return "測試@Bean註解";
}
}
結果:
4.配置檔案application.properties中的佔位符–隨機數
javabean實體類:
package qing.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "stu")
@PropertySource(value = {"classpath:student.properties"})
public class Student {
private int stuid;
private String stuname;
private double stuheight;
private String stuaddress;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public double getStuheight() {
return stuheight;
}
public void setStuheight(double stuheight) {
this.stuheight = stuheight;
}
public String getStuaddress() {
return stuaddress;
}
public void setStuaddress(String stuaddress) {
this.stuaddress = stuaddress;
}
}
application.properties 配置檔案:
stu.stuid=${random.int}
stu.stuname=lisi
stu.stuheight=${random.int[1024,65536]}
stu.stuaddress=${stu.stuname}_address
控制器類:
package qing.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import qing.bean.Student;
@RestController
public class StudentController {
@Autowired
private Student student;
@RequestMapping("/student")
public String getStudent(){
int id=student.getStuid();
String name=student.getStuname();
double height=student.getStuheight();
String address=student.getStuaddress();
String info=id+name+height+address;
System.out.println(id+" "+name+" "+height+" "+address);
return info;
}
}
結果:
5.Profiles
Profifile是用來配置在不同環境下的配置資料,因為在不同的環境下配置檔案中配置的執行環境的資料是不同的,需要靈活的在不同的執行環境下切換成對應的執行環境的資料,需要將不同的執行環境資料配置到不同的配置檔案中,通過在主配置檔案application.properties中的spring.profiles.active屬性完成切換。
例如:
application-prod.properties
server.port=9090
application-dev.properties
server.port=8090
application.properties
spring.profiles.active=dev
結果:伺服器預設是8080埠號,通過配置切換成指定8090埠號
application-prod.properties
server.port=9090
application-dev.properties
server.port=8090
application.properties
spring.profiles.active=prod
結果:伺服器預設是8080埠號,通過配置切換成指定9090埠號
6.主配置檔案載入位置
spring boot 啟動會掃描以下位置的application.properties或者 application.yml檔案作為Spring boot的預設配置檔案 。
– 專案根目錄/config/
– 專案根目錄/
– resource/config/
– resource:/
以上是按照優先順序從高到低的順序,所有位置的檔案都會被載入,出現相同配置內容時,高優先順序配置內容會覆蓋低優先順序配置內容,配置內容不同時進行配置互補。
可以通過配置spring.config.location來改變預設配置,專案打包好以後,我們可以使用命令列引數的形式,啟動專案的時候來指定配置檔案的新位置,指定配置檔案和預設載入的這些配置檔案共同起作用形成互補配置。
java -jar testspringboot02-0.0.1-SNAPSHOT.jar --spring.confifig.location=F:/application.properties
外部配置載入順序
Spring Boot 支援多種外部配置方式
(1)、命令列引數
(2)、來自java:comp/env的JNDI屬性
(3)、Java系統屬性(System.getProperties())
(4)、作業系統環境變數
(5)、RandomValuePropertySource配置的random.*屬性值
(6)、jar包外部的application-{profile}.properties或application.yml(帶spring.profile)配置檔案
(7)、jar包內部的application-{profile}.properties或application.yml(帶spring.profile)配置檔案
(8)、jar包外部的application.properties或application.yml(不帶spring.profile)配置檔案
(9)、jar包內部的application.properties或application.yml(不帶spring.profile)配置檔案
優先載入帶profifile,再來載入不帶profifile,由jar包外向jar包內進行尋找
(11)、@Configuration註解類上的@PropertySource
(12)、通過SpringApplication.setDefaultProperties指定的預設屬性