Spring Boot——读取属性文件的多种方法

前言

在程序开发中,为了减少程序中的“硬编码”,我们通常会将一些信息放在属性文件中,再读取到程序中。Spring 提供了多种便利的方法帮助我们从属性文件中读取数据。

一、属性文件加载

程序默认加载application.yml(或application.properties)属性文件。如需要加载其它属性文件使用@PropertySource注解:
例如: 加载user.properties文件

@PropertySource("user.properties")

加载完属性文件后我们可以通过以下集中方式获取属性设置的值

二、读取属性值的方式
1、Environment检索属性
@Autowired
	private Environment env;
	@Bean
	@Qualifier("env")
	public User getUserByEnv() {
		User user = new User();
		// 获取属性值
		user.setUsername(env.getProperty("com.xzx.username"));
		user.setSay(env.getProperty("com.xzx.say"));
		return user ;
	}

Environment类对属性的操作有多种方法,比如对null属性赋予默认值,按指定数据类型读取属性,检查属性是否存在等;具体可以详细了解Environmentment类

2、@ConfigurationProperties方式

使用@configuration注解可以读取文件中的属性,根据属性key自动为Bean赋值;如下我们注入一个User bean并通过该注解为其属性赋值。

注:User bean的属性名与文件中的属性名一致;

@Bean
	@Qualifier("conPro")
	@ConfigurationProperties(prefix="com.xzx")
	public User getUserByCP() {
		return new User();
	}

User.java

package com.facets.core.entity;


public class User {
	private String username;
	private String say;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getSay() {
		return say;
	}
	public void setSay(String say) {
		this.say = say;
	}
}

3、@Value方式

我们还可以使用属性占位符的方式获取属性值;

@Value("${com.xzx.username}")
	private String username;
	
	@Value("${com.xzx.say}")
	private String say;
	
	@Bean
	@Qualifier("value")
	public User getUserByValue() {
		User user = new User();
		user.setUsername(username);
		user.setSay(say);
		return user ;
	}

以上三种方式合并文件为

package com.facets.core.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.facets.core.entity.User;

@Configuration
// 默认加载application属性文件
@PropertySource(value="user.properties")
public class UserConfig {
	@Autowired
	private Environment env;
	@Bean
	@Qualifier("env")
	public User getUserByEnv() {
		User user = new User();
		user.setUsername(env.getProperty("com.xzx.username"));
		user.setSay(env.getProperty("com.xzx.say"));
		return user ;
	}
	
	@Bean
	@Qualifier("conPro")
	@ConfigurationProperties(prefix="com.xzx")
	public User getUserByCP() {
		return new User();
	}
	
	@Value("${com.xzx.username}")
	private String username;
	
	@Value("${com.xzx.say}")
	private String say;
	
	@Bean
	@Qualifier("value")
	public User getUserByValue() {
		User user = new User();
		user.setUsername(username);
		user.setSay(say);
		return user ;
	}
}

三、测试

属性文件user.properties

com.xzx.username=xzx
com.xzx.say=hello worldFacets

FacetsApplication.java

package com.facets.core;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.facets.core.entity.User;

@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
@RestController
public class FacetsApplication {

	@Autowired
	@Qualifier("env")
	private User user;
	
	@Autowired
	@Qualifier("conPro")
	private User user1;
	
	@Autowired
	@Qualifier("value")
	private User user2;
	
	@RequestMapping("/test")
	public List<User> getUser() {
		List<User> list = new ArrayList<>();
		list.add(user);
		list.add(user1);
		list.add(user2);
		return list;
	}
	public static void main(String[] args) {
		SpringApplication.run(FacetsApplication.class, args);
	}
}

输出:

[{"username":"xzx","say":"hello world"},{"username":"xzx","say":"hello world"},{"username":"xzx","say":"hello world"}]

许振雪,2018.11.3
Xu Blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值