`
234390216
  • 浏览: 10218594 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
博客专栏
A5ee55b9-a463-3d09-9c78-0c0cf33198cd
Oracle基础
浏览量:462009
Ad26f909-6440-35a9-b4e9-9aea825bd38e
springMVC介绍
浏览量:1774424
Ce363057-ae4d-3ee1-bb46-e7b51a722a4b
Mybatis简介
浏览量:1397489
Bdeb91ad-cf8a-3fe9-942a-3710073b4000
Spring整合JMS
浏览量:394699
5cbbde67-7cd5-313c-95c2-4185389601e7
Ehcache简介
浏览量:679501
Cc1c0708-ccc2-3d20-ba47-d40e04440682
Cas简介
浏览量:530288
51592fc3-854c-34f4-9eff-cb82d993ab3a
Spring Securi...
浏览量:1180700
23e1c30e-ef8c-3702-aa3c-e83277ffca91
Spring基础知识
浏览量:465997
4af1c81c-eb9d-365f-b759-07685a32156e
Spring Aop介绍
浏览量:151028
2f926891-9e7a-3ce2-a074-3acb2aaf2584
JAXB简介
浏览量:67732
社区版块
存档分类
最新评论

Spring(20)——@PropertySource

阅读更多

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所写)

0
0
分享到:
评论

相关推荐

    SSH笔记-通过property-placeholder使用外部属性文件

    本笔记将专注于Spring框架中的一个关键特性——通过`property-placeholder`使用外部属性文件,这在实际项目中非常常见,能够有效地实现配置的解耦和管理。 首先,让我们理解`property-placeholder`的概念。在Spring...

    com.smart.spring.zip

    同时,`@Import`可以引入其他配置类,`@PropertySource`加载属性文件,`@Value`注入属性值,这些注解让配置变得动态且易于理解。 4. **测试支持**: 压缩包可能还包含了使用`@RunWith(SpringRunner.class)`、`@...

    Spring.NET实用技巧2——NHibernate访问Oracle

    &lt;property name="ConnectionString" value="Data Source=yourOracleDB;User Id=yourUsername;Password=yourPassword;" /&gt; &lt;property name="ConfigLocation" value="assembly://YourAssembly/hibernate.cfg.xml" /&gt;...

    spring-framework-reference 3.0

    Spring 3.0 中引入了一个新的表达式语言——**Spring Expression Language (SpEL)**。这是一种强大的脚本语言,用于在Spring组件之间描述任务和配置。SpEL可以更好地与XML配置文件交互,并且对于安全性和集成方面...

    《Spring+in+action+中文版(第4版)》读书笔记

    1. **Java配置**:使用`@PropertySource`注解和`Environment`接口。 2. **XML配置**:使用`&lt;context:property-placeholder&gt;`标签,并通过`${property}`的方式引用属性值。 #### 七、Spring Expression Language ...

    spring_day02_spring_

    标题“spring_day02_spring_”和描述“heima spring source code day 02”表明这是一份关于Spring框架的深入学习资料,主要聚焦在源码分析上,特别是针对第一天学习后的第二天内容。标签“spring”进一步确认了讨论...

    spring-developing-java-applications-enterprise

    3. **使用Spring Source Tool Suite (STS)**:这是一个专为Spring开发设计的IDE,能够极大地提高开发效率。 4. **创建Spring项目**: - 在IDE中新建一个Spring项目。 - 添加所需的库文件,例如Spring Core、Spring...

    spring-core组件详解——PropertyResolver属性解决器

    这个类利用了`PropertySources`集合,这是一个包含多个`PropertySource`的对象列表。它会按照顺序遍历`PropertySource`,查找并返回第一个非`null`的属性值。`PropertySource`可以是从各种来源(如属性文件、系统...

    Spring入门

    - **示例**:定义一个属性文件,使用`@PropertySource`注解加载该文件,并通过`@Value`注解将属性值注入到bean中。 #### 集合对象注入 - **定义**:在Spring配置中,可以将List、Set、Map等集合类型的数据注入到...

    在SpringBoot下读取自定义properties配置文件的方法

    import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import lombok.Data; @Data @Configuration @ConfigurationProperties(prefix = "remote", ...

    从零搭建Spring Boot脚手架,手写Mybatis通用Mapper.docx

    在之前的篇章中,我们已经集成了国内广受欢迎的ORM框架——Mybatis。然而,在实际开发过程中,为了进一步提升效率,我们希望实现一些开箱即用的通用Mapper。本文将分享这一尝试的过程与心得,并提供相关代码示例。 ...

    +Flex+集成到+Java+EE+应用程序的最佳实践(完整源代码)

    Property 的 SOURCE 属性由 BlazeDS 读取 XML 配置文件获得: 清单 12. 配置 destination 的 id &lt;destination id="flexService"&gt; &lt;properties&gt; &lt;factory&gt;flexFactory&lt;/factory&gt; &lt;source&gt;flexService&lt;/...

    将 Flex 集成到 Java EE 应用程序的最佳实践(完整源代码)

    Property 的 SOURCE 属性由 BlazeDS 读取 XML 配置文件获得: 清单 12. 配置 destination 的 id &lt;destination id="flexService"&gt; &lt;properties&gt; &lt;factory&gt;flexFactory&lt;/factory&gt; &lt;source&gt;flexService&lt;/...

    Struts技术经典入门

    本教程将带你迈出学习Struts的第一步——创建一个简单的“Hello World”程序。 1. 新建Web工程 创建一个新的Web工程是Struts入门的基础。首先,在Eclipse或类似的IDE中,选择新建Web工程。在新建工程对话框中,...

Global site tag (gtag.js) - Google Analytics