1. 程式人生 > >3springboot:springboot配置檔案(配置檔案、YAML、屬性檔案值注入<@Value、@ConfigurationProperties、@PropertySource,@ImportResource、@Bean>)

3springboot:springboot配置檔案(配置檔案、YAML、屬性檔案值注入<@Value、@ConfigurationProperties、@PropertySource,@ImportResource、@Bean>)

1.配置檔案:

springboot預設使用一個全域性配置檔案 配置檔名是固定的  

 配置檔案有兩種(開頭均是application,主要是檔案的字尾):

->application. properties ->application. yml

 

作用:修改springboot自動配置的預設值

      springboot在底層把一切都自動配好

 

位置:

  配置檔案放在src/main/resourcr目錄或者 類路徑/config 下

 

2.YAML:

YAML(YAML Ain't Markup Language)

YAML A Markup Language:是一個標記語言
YAML isn't Markup Language:不是一個標記語言;

 

標記語言:
以前的配置檔案;大多都使用的是 xxxx.xml檔案;
YAML:以資料為中心,比json、xml等更適合做配置檔案;

參考:http://yaml.org/

該語法風格:

server:
  port: 8088

 

使用語法:

  k:(空格)v:表示一對鍵值對(空格必須有);

以空格的縮排來控制層級關係;只要是左對齊的一列資料,都是同一個層級的

 

值的寫法:

字面量:普通的值(數字,字串,布林)
  k: v:字面直接來寫;
  字串預設不用加上單引號或者雙引號;
  " ":雙引號;不會轉義字串裡面的特殊字元;特殊字元會作為本身想表示的意思
    name: "zhangsan \n lisi":輸出;zhangsan 換行 lisi
  ' ':單引號;會轉義特殊字元,特殊字元最終只是一個普通的字串資料
    name: ‘zhangsan \n

1.物件、Map(屬性和值)(鍵值對):

注意空格

Person:
 name:xxxx
 age:12

行內寫法:

  Person:  {name:xxx,age=12
}

2.陣列(List、Set):

-(空格) 值表示陣列中的一個元素
gender:     -(空格) boy     -(空格) gril

行內寫法

gender: [gril,boy]

 

匯入配置檔案處理器,配置檔案進行繫結就會有提示

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

 

3.程式碼測試

//將配置檔案中的每一個屬性的值對映到這個元件中
//告訴springboot將本類中的所有屬性和配置檔案中的相關配置進行繫結
//(prefix = "person")將配置檔案中以person下的所有屬性進行繫結
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private  String name;
    private  Integer age;
    private boolean boss;//布林值
    private Date bir;//時間

    private Map<String,Object> map;//Map
    private List<String> lists;//List

    private Dog dog;//物件
}
public class Dog {
    private String name;
    private  Integer age;
}

 

在yml配置檔案中

person:
  name: Mr
  age: 14
  boss: true
  bir: 2018/12/21
  map: {m1: v1,m2: v2}
  lists:
     - mc
     - mt
  dog:
     name: dogdog
     age: 10

測試:

@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot01ApplicationTests {
    @Autowired
    Person person;
    @Test
    public void contextLoads() {
        System.out.println(person);
    }

}
Person{name='Mr', age=14, boss=true, bir=Fri Dec 21 00:00:00 CST 2018, map={m1=v1, m2=v2}, 
lists=[mc, mt], dog=Dog{name='dogdog', age=10}}

 

 使用properties字尾的:

person.name=mr
person.age=22
person.bir=2018/12/11
person.boss=true
person.map.q1=1
person.map.q2=2
person.lists=a,b,c
person.dog.name=cat
person.dog.age=22

 

 

關於亂碼的問題:

properties配置檔案在idea中預設utf-8可能會亂碼

 

 

 4.配置檔案值注入@Value

 @Value:1.字面量  2.#{spel}    3.${key}從環境變數配置檔案中取值

@Value獲取值和@ConfigurationProperties獲取值比較

 

鬆散語法繫結:last_name = last-name = lastName 他們取的值都是相同的

配置檔案yml還是properties他們都能獲取到值;

如果說,我們只是在某個業務邏輯中需要獲取一下配置檔案中的某項值,使用@Value;

如果說,我們專門編寫了一個javaBean來和配置檔案進行對映,我們就直接使用@ConfigurationProperties;

 

 資料校驗:

@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
    @Email
    private  String name;
...
}

 

 獲取配置檔案中的某個值:

@ResponseBody
@Controller
public class Helloword {

    @Value("${person.name}")
    private String name;

    @RequestMapping("/hello")
    public  String hello(){
        return "Hello tow!" + name;
    }
}

 

[email protected],@ImportResource

@PropertySource: 載入指定的配置檔案
@PropertySource(value = {"classpath:/person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private  String name;
    private  Integer age;
    private boolean boss;
    private Date bir;
...
}

 

@ImportResource

@ImportResource:匯入Spring的配置檔案,讓配置檔案裡面的內容生效;
Spring Boot裡面沒有Spring的配置檔案,我們自己編寫的配置檔案,也不能自動識別;
想讓Spring的配置檔案生效,載入進來;@ImportResource標註在一個配置類上

@ImportResource(locations = {"classpath:spring.xml"})
@SpringBootApplication
public class Springboot01Application {
    public static void main(String[] args) {
        SpringApplication.run(Springboot01Application.class, args);
    }
}
public class helloword {
    public void hello(){
        System.out.println("hello");
    }
}

 

<?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="hello" class="com.cr.springboot01.ImportSoource.helloword"></bean>
</beans>
@Autowired
ApplicationContext app;
@Test
public void testImportSourcr(){
    boolean h = app.containsBean("hello");
    System.out.println(h);
}

 

 

@Bean

全註解方式:

新建一個配置類

//配置類,指明當前類使配置類,代替xml配置檔案
@Configuration
public class MyConfig {
    //將方法的返回值新增到容器中,容器中這個元件預設的id就是方法名
    @Bean
    public Helloword hello(){
        System.out.println("註解的方式");
        return new Helloword();
    }
}
@Autowired
ApplicationContext app;
@Test
public void testImportSourcr(){
    boolean h = app.containsBean("hello");
    System.out.println(h);
}