在Spring中使用@Value可以對單個屬性進行注入配置,但如果有很多配置屬性或者配置屬性本身擁有層級結構時,Spring Boot提供了基于類型安全的配置方式。本文系統的帶大家了解一下基于類型安全的屬性配置。
@ConfigurationProperties
通過@ConfigurationProperties将properties屬性和一個Bean及其屬性關聯,進而實作類型安全配置。
就像上面說的,如果在實踐的過程中如果有很多配置,通過@Value逐一配置不僅麻煩,還不太符合面向對象的程式設計思想。根據不同情況,可适當的将對應的配置檔案根據業務進行分組,集中管理。此時,基于類型安全的屬性配置便發揮了作用。
@ConfigurationProperties加載properties檔案内的配置,通過prefix屬性指定配置檔案中定義的properties配置的統一字首。
@ConfigurationProperties(prefix = "remote"})
如果對應配置檔案不是通過SpringBoot預設加載,Spring Boot1.5之前可通過locations指定properties文的位置,如下:
@ConfigurationProperties(prefix = "remote",locations={"classpath:remote.properties"})
在随後版本逐漸廢棄,原因很簡單,Spring Boot認為将一個配置類綁定到一個配置檔案不太合适。替代方案可以使用@PropertySource來指定自定義的資源目錄。
示例代碼
下面以具體的執行個體來示範一下如何使用@ConfigurationProperties。
remote.properties配置檔案中配置内容如下:
remote.address= www.choupangxia.com
remote.port= 8080
對應實體類如下:
@Component
@PropertySource({"classpath:remote.properties"})
@ConfigurationProperties(prefix = "remote")
public class RemoteConfig {
/**
* 遠端服務位址
*/
private String address;
/**
* 遠端服務端口
*/
private int port;
// getter/stetter方法
}
對應RemoteConfig的Bean的使用:
@RestController
public class ConfigurationController {
@Resource
private RemoteConfig remoteConfig;
@GetMapping
public void getInfo() {
System.out.println("位址:" + remoteConfig.getAddress());
System.out.println("端口:" + remoteConfig.getPort());
}
}
單元測試方法内容:
@SpringBootTest
@AutoConfigureMockMvc
class ConfigurationControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void getInfo() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/"));
}
}
執行,單元測試,日志列印如下:
位址:www.choupangxia.com
端口:8080
說明配置檔案中的配置已經完成注入。
通過上述配置為RemoteConfig建立了一個正常的bean,不過Spring Boot建議@ConfigurationProperties隻用來處理environment(隻用于注入配置,系統環境之類的),不要注入上下文中的其他beans。
第三方配置
@ConfigurationProperties不僅可以注解在類上,也可以注解在public @Bean方法上,當需要為不受控的第三方元件綁定屬性時,該方法将非常有用。
配置檔案中的屬性定義沒有差別,注解具體使用如下:
@Configuration
public class MyConfig {
@Bean
@ConfigurationProperties(prefix = "user")
public User user() {
return new User();
}
}
松散的綁定限制
Spring Boot将Environment屬性綁定到@ConfigurationProperties Bean時會使用一些寬松的規則,是以Environment屬性名和Bean屬性名不需要精确比對。
比如在對象User中有一個firstName屬性,那麼在配置檔案中對應如下配置項均會比對:
user.firstName // 标準駝峰命名文法
user.first-name // 短橫線隔開表示,推薦用于.properties和.yml檔案中
user.first_name // 下劃線表示,用于.properties和.yml檔案的可選格式
USER_FIRST_NAME // 大寫形式,推薦用于系統環境變量
@ConfigurationProperties校驗
當類上标注了@Validated注解,Spring Boot會嘗試校驗@ConfigurationProperties注解的類。可以在配置類中直接使用JSR-303 javax.validation限制标注。使用之前確定在類路徑中存在适用的JSR-303實作:
@Component
@PropertySource({"classpath:remote.properties"})
@ConfigurationProperties(prefix = "remote")
@Validated
public class RemoteConfig {
@NotNull
private String phone;
// getter/setter
}
自定義yml檔案支援
上面我們提到如果配置檔案未配置在預設的application檔案内,則需要使用@PropertySource進行指定加載。但如果此時如果你使用的是yml格式的檔案,會發現使用@PropertySource無法加載對應的檔案。這是因為預設情況下@PropertySource并不支援yml檔案的解析。
是以,如果在項目中使用的是yml格式的自定義配置檔案,可自定義PropertySourceFactory來進行支援。
public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
Properties propertiesFromYaml = loadYamlIntoProperties(resource);
String sourceName = name != null ? name : resource.getResource().getFilename();
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
}
private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
try {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
} catch (IllegalStateException e) {
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException) {
throw (FileNotFoundException) e.getCause();
}
throw e;
}
}
}
@PropertySource(value = "classpath:remote.yml",factory = YamlPropertyLoaderFactory.class)