個人博客:haichenyi.com。感謝關注
用IDEA可以直接創建SpringBoot項目,創建Moudle的時候,在選Maven的地方,選擇Sping Initializr即可,很簡單。前面講過了IDEA生成Maven項目,並且嵌入SpringBoot,直接創建Spring Boot項目就是IDEA直接幫我們依賴,並且生成Application和properties文件。其它,也沒啥大的區別,就不多說了。
本篇要講的是SpringBoot的配置文件,分爲兩種:
- properties
- yml
之前,搜SpringBoot的配置文件怎麼寫的時候,看到網上很多博客都有文件內容,但是有兩種寫法,就是這兩種配置文件的不同寫法。
舉個栗子,我現在要修改服務器啓動的端口號,用properties怎麼修改呢?很簡單:
如上圖所示,是不是很簡單?
server.port=8081
用yml怎麼修改呢?也不難:
如上圖所示:也不難
server:
port: 808
看到了上面兩個圖片,應該也已經發現了,兩個配置文件存放位置都在resource目錄下面。或者在類路徑的"/config"路徑下
這兩種寫法要怎麼寫呢?
properties: 等號連接,右邊是值
yml:key:
- value 表示一對鍵值對(冒號後面必須要有空格)
- 使用空格縮進表示層級關係
- 左側縮進的空格數目不重要,只要同一層級的元素左側對齊即可
- key 與 value 大小寫敏感
PS: yml不管是存map,還是list,都要記得key後面的冒號一定要跟空格,再寫值
舉個栗子,我們定義一個bean類,在配置文件裏面賦值,在項目裏面取出來用。比方說:我們項目裏面定義一個Userbean類。
package com.haichenyi.springboot.pojo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private String username;
private String nickname;
private String password;
private Integer age;
}
這裏有幾點需要注意:
- 這裏的bean類,必須要有寫set/get方法,不然賦不了值,我這裏沒寫是因爲我用的@Data這個註解,它默認幫我們創建了set/get方法
- @ConfigurationProperties註解就是 告訴SpringBoot將配置文件中對應屬性的值,映射到這個組件類中,進行一 一綁定,prefix = "user"的作用就是配置文件中的前綴名,哪個前綴與下面的所有屬性進行一一映射
- @Component 註解就是將當前組件也就是這個bean類作爲SpringBoot中的一個組件,才能使用容器提供的
然後就是在配置文件中賦值,properties或者是yml,都可以:
properties中:
server.port=8081
user.username=s19734682s
user.age=20
user.password=123456
user.nickname=林汐痕
yml中:
server:
port: 8088
//這個user就是上面說的配置文件中的前綴名
user:
nickname: 海晨憶
username: pk19734682
password: 123456
age: 18
怎麼使用呢?直接在controller中用這個user就可以了
package com.haichenyi.springboot.controller;
import com.haichenyi.springboot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class SayController {
@Autowired
private User user;
@ResponseBody
@RequestMapping("/say")
public String say() {
return "hello " +user;
}
}
就這樣,請求這個接口就能打印值。
還有通過@Value指定值的方式
@Controller
public class SayController {
@Value("${user.nickname}")
private String nickname;
@ResponseBody
@RequestMapping("/say")
public String say() {
return "hello " + nickname;
}
}
@PropertySource 加載局部配置文件
什麼叫加載局部配置文件呢?就是加載指定的配置文件,並不是從properties或者是yml中加載。怎麼加載呢?如下圖:
第一步, 在resources目錄下創建xxx.properties/xxx.yml,這個xxx你自己命名。我這裏創建的是user.properties/user.yml。記得語法不要寫錯了
第二步, 在你的組件的地方加上@PropertySource註解。圖上有,可以導入多個局部配置,用逗號隔開,每個局部配置的寫法就是 “classpath:文件名” 之前導入全局的配置的時候加的註解也需要,不能刪除。
第三步, 之前是怎麼使用的,現在還是怎麼使用。
@ImportResource加載xml配置文件
Spring Boot框架並不推薦用xml加載配置文件,這個是Spring加載文件的方式。項目中如果必須要用到xml加載文件,要怎麼辦呢?
舉個栗子:我要加載一個Service類到項目中。Spring Boot有註解可以直接使用,這裏,我們通過xml加載。
第一步, 創建一個service。
package com.haichenyi.springboot.service;
public class UserService {
public void say() {
System.out.println("xml...");
}
}
第二步, 在resources目錄下,創建spring config的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="userService" class="com.haichenyi.springboot.service.UserService"></bean>
</beans>
如上,添加了一個bean類,兩個屬性,id和class。class指向剛纔創建的類,id用於獲取這個類。
第三步, 在我們的引導類中加上 @ImportResource 註解
package com.haichenyi.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@ImportResource(locations = "classpath:springboot01.xml")
@SpringBootApplication
public class SpringBootInitApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootInitApplication.class, args);
}
}
第四步, 通過ApplicationContext的getBean方法獲取,傳的參數就是在xml中定義的id。如下:
package com.haichenyi.springboot;
import com.haichenyi.springboot.pojo.User;
import com.haichenyi.springboot.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootInitApplicationTests {
@Autowired
User user;
@Test
public void contextLoads() {
System.out.println(user);
}
@Autowired
ApplicationContext context;
@Test
public void textXml() {
UserService userService = (UserService) context.getBean("userService");
userService.say();
}
}
自定義配置類向容器中注入組件(SpringBoot推薦)
舉個栗子:跟上面xml的例子一樣。
第一步, 跟上面一樣,創建一個service
第二步, 創建配置類:
package com.haichenyi.springboot.custom;
import com.haichenyi.springboot.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
@Bean
public UserService userService2() {
return new UserService();
}
}
需要注意的地方:
- @Configuration 用於標識當前類是一個配置類,用來表示對應spring配置文件
- @Bean 標識的方法用於向容器注入組件
- 方法的返回值就是注入容器中的組件對象
- 方法名是這個組件對象的 id值
第三步, 就是使用了,跟上面第四步一樣。不需要上面的第三步。
Profile多環境支持
profile是Sping用來針對不同的環境要求,提供不同的配置支持。什麼不同的環境呢?比方說:開發環境,生產環境。
全局 Profile 配置使用的文件名可以是
application-{profile}.properties / application-{profile}.yml 。如:application-dev.properties / application-prod.properties
舉個栗子:
我們的項目環境分爲 開發 (dev)和 生產(prod)環境 ,開發環境下端口號爲 8081,
生產環境下端口號爲8082。
通過properties文件指定
如上圖,創建了兩個文件:application-dev.properties,application-prod.properties,裏面內容很簡單,就是指定端口號。server.port=8081
然後,我們在application.properties文件中指定激活哪一個文件即可。
//激活application-dev.properties配置
spring.profiles.active=dev
//激活application-prod.properties配置
spring.profiles.active=prod
通過yml文件指定
不用新建文件,直接在application.yml寫就可以了。
PS:需要用三個減號隔開,表示不同的文檔塊。
spring:
profiles:
active: prod #激活哪個profile , 當前激活的是 prod 開發環境
---
server:
port: 8081
spring:
profiles: dev #指定屬於哪個環境, dev 環境時使用
---
server:
port: 8081
spring:
profiles: prod #指定屬於哪個環境, prod 環境時使用