20 @PropertySource
在之前介绍<context:property-placeholder/>
时提到过其默认会使用PropertySourcesPlaceholderConfigurer来进行对应的属性替换,其底层有使用PropertySource。@PropertySource是用来注册一个PropertySource的。PropertySource是用来表示一个name/value属性配对的资源的,可以简单的把它理解为我们熟悉的Properties。
Environment可以持有一系列的PropertySource,然后在从中获取属性时,其会依次从对应的PropertySource中寻找,当然也包括系统属性和环境变量。一个Environment中默认会包含两个PropertySource,分别对应于系统属性和环境变量。即默认情况下在只有系统属性和环境变量对应的两个ProperySource时,如果我们从Environment中获取某属性,将先从系统属性中取,没取到再从环境变量中获取。所以,如下示例我们直接从Environment中获取属性“user.dir”的值,这里取的就是系统属性“user.dir”的值。
@Test
public void testPropertySource() {
ConfigurableApplicationContext context = new GenericApplicationContext();
ConfigurableEnvironment env = context.getEnvironment();
String userDir = env.getProperty("user.dir");
System.out.println(userDir);
context.close();
}
我们也可以往Environment中添加PropertySource对象,之后添加的PropertySource对象就可以用来获取对应的属性。如下示例中我们往Environment中添加了一个基于类路径下的init.properties文件的ResourcePropertySource,且是加在所有的PropertySource之前,由于在从Environment中获取属性时,将优先从前面的PropertySource中获取。那么如果我们在init.properies文件中定义了一个user.dir属性,则下面示例中获取到的user.dir就将是我们在init.properties文件中指定的那个。
@Test
public void testPropertySource() throws IOException {
ConfigurableApplicationContext context = new GenericApplicationContext();
//获取Environment对象
ConfigurableEnvironment env = context.getEnvironment();
//获取Environment的PropertySources
MutablePropertySources propertySources = env.getPropertySources();
//new一个基于Resource的PropertySource
ResourcePropertySource rps = new ResourcePropertySource(new ClassPathResource("init.properties"));
//给当前Environment对象env添加一个PropertySource对象
propertySources.addFirst(rps);
String userDir = env.getProperty("user.dir");
System.out.println(userDir);
context.close();
}
接下来我们来介绍一下@PropertySource。@PropertySource需要和@Configuration一起使用。其可以让我们非常方便的把外部资源定义封装成一个PropertySource对象添加到对应的Environment中。如下示例中我们通过在使用@Configuration进行标注的配置类上通过@PropertySource标注引入了一个类路径下的init.properties文件作为一个PropertySource添加到了当前的Environment中(当指定的资源未指定前缀时默认就会当做ClassPathResource处理)。之后我们在创建bean时就可以使用Environment对象来获取其中拥有的PropertySource中定义的属性对应的属性值,如下述示例中我们在创建hello时就从Environment中获取了属性“hello.name”的值。
@Configuration
@PropertySource("init.properties")
public class SpringConfig {
@Autowired
private Environment env;
@Bean
public Hello hello() {
String helloName = env.getProperty("hello.name");
Hello hello = new Hello(helloName);
return hello;
}
}
如果需要同时将多个外部文件作为PropertySource添加到对应的Environment中,则我们可以通过@PropertySource的value属性指定多个资源,其对应于一个数组。如下示例中我们就同时将类路径下的init.properties和init2.properties文件作为PropertySource添加到当前的Environment中。
@Configuration
@PropertySource({"init.properties", "init2.properties"})
public class SpringConfig {
}
我们也可以通过@PropertySources注解来定义多个@PropertySource,以添加多个PropertySource到Environment中。
@Configuration
@PropertySources({@PropertySource("init.properties"), @PropertySource("init2.properties")})
public class SpringConfig {
}
默认情况下我们通过@PropertySource指定的资源是必须存在的,否则Spring将抛出异常,当然我们也可以通过@PropertySource的ignoreResourceNotFound属性来指定是否忽略资源未找到的情况,默认为false,表示不忽略。如下示例中我们就通过ignoreResourceNotFound指定了忽略init.properties文件不存在的情况。
@Configuration
@PropertySource(value="init.properties", ignoreResourceNotFound=true)
public class SpringConfig {
}
(注:本文是基于Spring4.1.0所写)
相关推荐
Spring boot——@DeclareParents例子...
NULL 博文链接:https://snowolf.iteye.com/blog/1481442
主要介绍了Spring @value和@PropertySource注解使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring4 In Action-3.5.1-@PropertySource运行时注入值,Spring4 In Action-3.5.1-@PropertySource运行时注入值,Spring4 In Action-3.5.1-@PropertySource运行时注入值,Spring4 In Action-3.5.1-@PropertySource...
Spring Boot 技术知识点:如何理解@PropertySource注解
Spring@PropertySource 配置文件自定义加密、自定义Yaml文件加载
目录一、Spring中的Profile二、为什么要使用Profile三、配置Profile示例四、激活Profile的方式 一、Spring中的Profile Spring中的Profile功能可以理解为我们在Spring容器中所定义的Bean的逻辑组名称,只有当这些...
介绍一下@PropertySource注解的用法,通过上一篇博客的知识,可以知道@Value和@ConfigurationProperties都可以用于获取配置文件的属性值,不过有个细节容易被忽略,那就是, 这两个注解在Springboot项目中都是获取...
NULL 博文链接:https://snowolf.iteye.com/blog/1628861
SpringCloud——分布式配置中心(Spring Cloud Config)之高可用的分布式配置中心
Spring3事务管理——使用@Transactional 注解.rar
SpringCloud——消息总线(Bus)之Spring Cloud Bus将分布式系统的节点与轻量级消息代理链接。
关于Spring方面的常见面试题
SpringCloud——断路器(Hystrix)之Ribbon使用断路器和Feign使用断路器
SpringCloud——分布式跟踪(Sleuth)之Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案。
SpringCloud——服务注册(consul)之Consul通过HTTP API和DNS提供服务发现服务。
SpringCloud——路由器和过滤器(Zuul)之微服务网关的实现
Spring系列——MVC框架整合.md
MyBatis与Spring整合——通过官方文档进行最简单的整合
主要介绍了Spring Boot自定义配置属性源(PropertySource),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧