您好,登录后才能下订单哦!
这篇文章主要介绍“SpringBoot获取配置文件内容的方式有哪些”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringBoot获取配置文件内容的方式有哪些”文章能帮助大家解决问题。
现有配置文件如下,如何获取到配置文件的值呢?
file: windows: D:\file linux: /usr/local/file
首先,可以标注到实体类上。
@Data
@Component
@ConfigurationProperties(prefix = "file")
public class FileProperties {
private String windows;
private String linux;
}标注在配置类上的方法上,同样是从配置文件中取值赋值到返回值的属性中。使用如下:
@Bean
@ConfigurationProperties(prefix = "userinfo")
public FileProperties fileProperties() {
return new FileProperties();
}使用方法:
@Service
public class Test {
@Autowired
private FileProperties fileProperties;
@Override
public void test() {
System.out.println(fileProperties.getLinux());
}
}总结
@ConfigurationProperties注解能够很轻松的从配置文件中取值,优点如下:
支持批量的注入属性,只需要指定一个前缀 prefix
支持复杂的数据类型,比如 List 、 Map
对属性名匹配的要求较低,比如user-name,user_name,userName,USER_NAME 都可以取值
支持JAVA的JSR303数据校验
@Value("${file.windows}")
private String windows;
@Value("${file.linux}")
private String linux;Spring Boot在启动的时候会自动加载 application.xxx 和 bootsrap.xxx ,但是为了区分,有时候需要自 定义一个配置文件,那么如何从自定义的配置文件中取值呢?
只需要在启动类上标注 @PropertySource 并指定你自定义的配置文件即可完成。
@SpringBootApplication
@PropertySource(value = {"classpath:custom.properties"})
public class DemoApplication{ }value属性是一个数组,可以指定多个配置文件同时引入。@PropertySource默认加载xxx.properties类型的配置文件,不能加载YML格式的配置文件。
@PropertySource注解有一个属性 factory ,默认值是PropertySourceFactory.class,这个就是用来加 载properties格式的配置文件,我们可以自定义一个用来加载 YML 格式的配置文件,如下:
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
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.Properties;
public class YmlConfigFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws
IOException {
String sourceName = name != null ? name : resource.getResource().getFilename();
if (!resource.getResource().exists()) {
return new PropertiesPropertySource(sourceName, new Properties());
} else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
Properties propertiesFromYaml = loadYml(resource);
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
} else {
return super.createPropertySource(name, resource);
}
}
private Properties loadYml(EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}此时只需要将 factory 属性指定为 YmlConfigFactory 即可,如下:
@SpringBootApplication
@PropertySource(value = {"classpath:custom.yml"}, factory = YmlConfigFactory.class)
public class DemoApplication { }@PropertySource 指定加载自定义的配置文件,默认只能加载 properties 格式,但是可以指定 factory 属 性来加载 YML 格式的配置文件。
关于“SpringBoot获取配置文件内容的方式有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。