Spring 在春季应用程序启动时加载yaml值
我有一些.yml文件,希望在应用程序启动时加载它们。我希望将它们加载到某个bean对象中。在此之后,我应该能够访问应用程序中任何地方的yml值。有可能吗 下面是ymlSpring 在春季应用程序启动时加载yaml值,spring,spring-boot,yaml,Spring,Spring Boot,Yaml,我有一些.yml文件,希望在应用程序启动时加载它们。我希望将它们加载到某个bean对象中。在此之后,我应该能够访问应用程序中任何地方的yml值。有可能吗 下面是yml Country: - CountryName: Afghanistan CountryCode: AFG CurrencyName: Afghan afghani CurrencyCode: AFN Region: Asia SubRegion: Southern Asia
Country:
-
CountryName: Afghanistan
CountryCode: AFG
CurrencyName: Afghan afghani
CurrencyCode: AFN
Region: Asia
SubRegion: Southern Asia
LanguageName: Pashto
LanguageCode: PUS
PerCapitaRank: 170
-
CountryName: Åland Islands
CountryCode: ALA
CurrencyName: Euro
CurrencyCode: EUR
Region: Europe
SubRegion: Northern Europe
LanguageName: Swedish
LanguageCode: SWE
PerCapitaRank:
是的,您可以使用
@ConfigurationProperties(prefix=“country”)
和注释将所有属性加载到bean类中,因为spring创建了bean,所以您可以在所需的位置自动连接这个bean
@Configuration
@ConfigurationProperties(prefix = "country")
public class countryListConfig {
private List<countryList> list;
public List<countryList> getList() {
return list;
}
public void setList(List<countryList> list) {
this.list = list;
}
public static class countryList {
private String CountryName;
private String CountryCode;
// getters and setters
}
@配置
@配置属性(前缀=“国家”)
公共类countryListConfig{
私人名单;
公共列表getList(){
退货清单;
}
公共无效集合列表(列表){
this.list=列表;
}
公共静态类countryList{
私有字符串CountryName;
私有字符串国家代码;
//接球手和接球手
}
若你们想在应用程序中的任何地方使用这些属性,那个么你们应该将它们声明为静态的,因为你们不能直接从yml文件自动连接静态变量,使用setter机制
private static List<countryList> staticlist;
public void setList(List<countryList> list) {
staticlist = list;
}
私有静态列表staticlist;
公共无效集合列表(列表){
静态列表=列表;
}
或者您也可以将
countryListConfig
bean自动连接到任何实用程序类中的静态引用,并在整个应用程序中使用该bean是的,您可以使用@ConfigurationProperties(prefix=“country”)将所有属性加载到bean类中
和注释,因为spring创建了bean,所以您可以在所需的位置自动连接这个bean
@Configuration
@ConfigurationProperties(prefix = "country")
public class countryListConfig {
private List<countryList> list;
public List<countryList> getList() {
return list;
}
public void setList(List<countryList> list) {
this.list = list;
}
public static class countryList {
private String CountryName;
private String CountryCode;
// getters and setters
}
@配置
@配置属性(前缀=“国家”)
公共类countryListConfig{
私人名单;
公共列表getList(){
退货清单;
}
公共无效集合列表(列表){
this.list=列表;
}
公共静态类countryList{
私有字符串CountryName;
私有字符串国家代码;
//接球手和接球手
}
若你们想在应用程序中的任何地方使用这些属性,那个么你们应该将它们声明为静态的,因为你们不能直接从yml文件自动连接静态变量,使用setter机制
private static List<countryList> staticlist;
public void setList(List<countryList> list) {
staticlist = list;
}
私有静态列表staticlist;
公共无效集合列表(列表){
静态列表=列表;
}
或者,您也可以将
countryListConfig
bean自动连接到任何实用程序类中的静态引用,并在整个应用程序中使用该bean我在上面尝试过,但它不起作用。我只使用了该bean,但没有使用Autowire。只需通知我的yml文件名是country.yml。这是导致问题的原因吗?请使用属性资源注释<代码>@PropertySource(name=“props”,value=“classpath:country.properties”,ignoreResourceNotFound=false)以及在yml中,将国家更改为国家。列表
我在上面尝试过,但不起作用。我只使用了它,但没有使用autowire。只是通知我的yml文件名是country.yml。是它导致了问题吗?使用属性资源注释@PropertySource(name=“props”,value=“classpath:country.properties”,ignoreResourceNotFound=false)
以及在yml中将country
更改为country.list