Spring注解驱动之@PropertySource加载配置文件

导读:本篇文章讲解 Spring注解驱动之@PropertySource加载配置文件,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

概述

@PropertySource注解是Spring 3.1开始引入的配置类注解。通过@PropertySource注解可以将properties配置文件中的key/value存储到Spring的Environment中,Environment接口提供了方法去读取配置文件中的值,参数是properties配置文件中定义的key值。当然,我们也可以使用@Value注解用${}占位符为bean的输入注入值。
我们来看一下@PropertySource注解的源代码,如下所示。
在这里插入图片描述
从@PropertySource的源码中可以看出,我们可以通过@PropertySource注解指定多个properties文件,使用的形式如下所示。

@PropertySource(value = {"classpath:/person.properties", "classpath:/car.properties"})

细心的读者可以看到,在@PropertySource注解的上面标注了如下的注解信息。

@Repeatable(PropertySources.class)

看到这里,小伙伴们是不是有种恍然大悟的感觉呢?没错,我们也可以使用@PropertySources注解来指定properties配置文件。
@PropertySources注解的源码比较简单,只有一个PropertySource[]数组类型的value属性,那我们如何使用@PropertySources注解指定配置文件呢?其实也很简单,使用如下所示的方式就可以了。

@PropertySources(value = {
	@PropertySource(value={"classpath:/person.properties"}),
	@PropertySource(value={"classpath:/car.properties"})
})

使用案例

准备工作

首先,我们在工程的src/main/resources目录下创建一个配置文件,例如person.properties,该文件的内容如下所示。

person.nickName=小甜甜

然后,我们在Person类中新增一个nickName字段,如下所示。

package com.meimeixia.bean;

import org.springframework.beans.factory.annotation.Value;

public class Person {
	
	@Value("李阿昀")
	private String name;
	@Value("#{20-2}")
	private Integer age;

	private String nickName; // 昵称
	
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Person(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", nickName=" + nickName + "]";
	}	
}

目前,我们并没有为Person类的nickName字段赋值,所以,此时Person类的nickName字段的值为空。我们可以运行IOCTest_PropertyValue类中的test01()方法来看下输出结果,如下所示。
在这里插入图片描述
可以看到,Person类的nickName字段的值确实输出了null。

使用注解方式获取值

如果我们使用注解的方式,那么该如何做呢?首先,我们需要在MainConfigOfPropertyValues配置类上添加一个@PropertySource注解,如下所示。

package com.meimeixia.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.meimeixia.bean.Person;

// 使用@PropertySource读取外部配置文件中的key/value保存到运行的环境变量中,加载完外部的配置文件以后,使用${}取出配置文件中的值
@PropertySource(value={"classpath:/person.properties"})
@Configuration
public class MainConfigOfPropertyValues {

	@Bean
	public Person person() {
		return new Person();
	}	
}

这里使用的@PropertySource(value={“classpath:/person.properties”})注解就相当于XML配置文件中使用的<context:property-placeholder location=“classpath:person.properties” />。

然后,我们就可以在Person类的nickName字段上使用@Value注解来获取person.properties文件中的值了,如下所示。

@Value("${person.nickName}")
private String nickName; // 昵称

配置完成后,我们再次运行IOCTest_PropertyValue类中的test01()方法来看下输出结果,如下所示。
在这里插入图片描述
可以看到,此时Person类的nickName字段已经注入小甜甜这个值了。

使用Environment获取值

上面我已经说过,使用@PropertySource注解读取外部配置文件中的key/value之后,是将其保存到运行的环境变量中了,所以我们也可以通过运行环境来获取外部配置文件中的值。

这里,我们可以稍微修改一下IOCTest_PropertyValue类中的test01()方法,即在其中添加一段使用Environment获取person.properties文件中的值的代码,如下所示。

@Test
public void test01() {
    printBeans(applicationContext);
    System.out.println("===================");
    Person person = (Person) applicationContext.getBean("person");
    System.out.println(person);
    
    ConfigurableEnvironment environment = applicationContext.getEnvironment();
    String property = environment.getProperty("person.nickName");
    System.out.println(property);
    // 关闭容器
    applicationContext.close();
}

在这里插入图片描述
可以看到,使用Environment确实能够获取到person.properties文件中的值。

参考

Spring注解驱动开发第19讲——使用@PropertySource加载配置文件,我只看这一篇!!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/99993.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!