3springboot:springboot配置文件(配置文件、YAML、屬性文件值注入<@Value、@ConfigurationProperties、@PropertySource,@Import

1.配置文件:java

springboot默認使用一個全局配置文件
配置文件名是固定的
 

 配置文件有兩種(開頭均是application,主要是文件的後綴):spring

->application. properties
->application. yml

 

做用:修改springboot自動配置的默認值json

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

 

位置:數組

  配置文件放在src/main/resourcr目錄或者 類路徑/config 下springboot

 

2.YAML:app

YAML(YAML Ain't Markup Language)ide

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

 

標記語言:
之前的配置文件;大多都使用的是 xxxx.xml文件;
YAML:以數據爲中心,比json、xml等更適合作配置文件;測試

參考:http://yaml.org/idea

該語法風格:

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;
    }
}

 

5@PropertySource,@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);
}

 

相關文章
相關標籤/搜索