1.@Value讀取
在springboot項目中,如果要讀取配置文件application.properties或application.yml文件的內容,可以使用自帶的注解@Value。以properties方式為例說明,yml方式同上:
1.1單層內容
application.properties配置文件內容:
name=huihui age=22 sex=1
讀取方式:
//導入的類org.springframework.beans.factory.annotation.Value; @Value("${name}") private String name; @Value("${age}") private Integer age; @Value("${sex}") private String sex;
1.2多層內容
application.properties配置文件內容:
user.name=huihui user.age=22 user.sex=1
讀取方式:
//導入的類org.springframework.beans.factory.annotation.Value; @Value("${user.name}") private String name; @Value("${user.age}") private Integer age; @Value("${user.sex}") private String sex;
對於多層內容,讀取時必須指定到最后一級。如上例必須到path,否則會報錯。實際上單層和多層沒有區別,只要指定到最后一級即可。
1.3設置默認值
對於有些配置,如果沒有在配置文件中配置,那么在啟動時就會報錯,它也可以設置默認值。
以上面的多層配置為例,並沒有配置user.addr的值,在注入時指定默認值:
@Value("${user.name}") private String name; @Value("${user.age}") private Integer age; @Value("${user.sex}") private String sex; @Value("${user.addr:湖北武漢}") private String addr;
由此可見,要指定默認值,只需要在注入的時候在參數后面添加“:”及默認值即可。
2.@Value+@PropertySource讀取
上述只是讀取application的配置文件,那么如何讀取自定義的配置文件呢?那就需要使用另外一個注解@PropertySource,指定配置文件的路徑和名稱。
2.1讀取properties文件內容
在resources目錄下新建testAAA.properties文件,內容如下:
user.name=huihui user.age=22 user.sex=1
使用組件的方式讀取:
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:testAAA.properties") public class TestService { @Value("${user.name}") private String name; @Value("${user.age}") private Integer age; @Value("${user.sex}") private String sex; public void test() { System.out.println(name); System.out.println(age); System.out.println(sex); } }
2.1讀取yml文件內容
對於yml的內容,如果還是采用上面的方式,是無法讀取到內容的,還會報錯。需要在上述基礎上指定自定義的工廠類。
1)新建一個類,繼承DefaultPropertySourceFactory類重寫其方法
package com.zys.springboottestexample.config; import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.core.env.PropertySource; import org.springframework.core.io.support.DefaultPropertySourceFactory; import org.springframework.core.io.support.EncodedResource; import java.io.IOException; import java.util.List; public class PropertySourceFactory extends DefaultPropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { if (resource == null) { return super.createPropertySource(name, resource); } List<PropertySource<?>> sources = new YamlPropertySourceLoader().load(resource.getResource().getFilename(), resource.getResource()); return sources.get(0); } }
2)在讀取時指自定義的工廠類
@Component @PropertySource(value="classpath:testAAA.yml", factory = PropertySourceFactory.class) public class TestService { @Value("${user.name}") private String name; @Value("${user.age}") private Integer age; @Value("${user.sex}") private String sex; public void test() { System.out.println(name); System.out.println(age); System.out.println(sex); } }
yml的內容同上,可粘貼:
user: name: huihui age: 22 sex: 1
3.@Value+@ConfigurationProperties讀取
3.1讀取默認的application.properties文件
當對同一對象有多個屬性時,每次在使用這些值時都重復的使用Value注解注入,有些麻煩,實際上也可以把這些屬性注入到bean上,需要使用的地方直接使用此bean即可。讀取application.yml文件可參考此方法,是一樣的讀取方式。
properties內容:
user.name=huihui user.age=22 user.sex=1
讀取內容:
@Component @ConfigurationProperties(prefix="user") @Data public class TestService { String name; String age; String sex; public void test() { System.out.println(name); System.out.println(age); System.out.println(sex); } }
需要注意的是,讀取時,屬性必須有set方法。
3.2讀取自定義的properties文件
要讀取自定義的文件,根據第二章的思想,如果可以指定文件的位置和名稱就可以讀取。但ConfigurationProperties注解並不能指定,這就需要PropertySource結合來指定自定義的配置文件。
@Component @ConfigurationProperties(prefix="user") @PropertySource("classpath:testAAA.properties") @Data public class TestService { private String name; private Integer age; private String sex; public void test() { System.out.println(name); System.out.println(age); System.out.println(sex); } }
對於yml的內容,參考第二章自行。