【springboot】自定义配置文件读取

方法一:@value

@RestController
@RequestMapping("/Hello")
public class HelloController {

	@Value("${people.name}")
	private String name;

	@Value("${people.country}")
	private String country;

	@RequestMapping("/print")
	public String printHello() {

		return name + "是" + country + "人";
	}
}
#application.properties
server.port=8081
server.context-path=/app
people.name=张飞
people.country=蜀国

在这里插入图片描述中文乱码了,网上给出了各种办法,都不可行,我的解决方案是不要在主配置文件application.properties中配置中文的属性值,可以单独新建一个配置文件,然后使用@PropertySource(value = {}, encoding = "utf-8")来引入新配置文件,这样就不会出现乱码了。

@RestController
@RequestMapping("/Hello")
@PropertySource(value = { "classpath:config.properties" }, encoding = "utf-8")
public class HelloController {

	@Value("${people.name}")
	private String name;

	@Value("${people.country}")
	private String country;

	@RequestMapping("/print")
	public String printHello() {

		return name + "是" + country + "人";
	}
}
#config.properties
people.name=张飞
people.country=蜀国

在这里插入图片描述

方法二:@ConfigurationProperties

使用@Value(${property})注释注入配置属性有时会很麻烦,特别是在处理多个属性数据本质上是分层的情况下。Spring Boot提供了另一种处理属性的方法@ConfigurationProperties,这种方法允许强类型bean控制和验证应用程序的配置,如下例所示:

@Component
@ConfigurationProperties(prefix = "people")
@PropertySource(value = { "classpath:config.properties" }, encoding = "utf-8")
public class PropertyConfig {

	private String name;

	private String countryName;

	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public String getCountryName() {
		return countryName;
	}

	public void setCountryName(final String countryName) {
		this.countryName = countryName;
	}
}
#config.properties
people.name=张飞
people.countryName=蜀国

@Component表明该配置类需要被spring容器加载。

@ConfigurationProperties(prefix = "people")该注解表明这是一个属性类,prefix表示所绑定的属性的前缀。

@PropertySource(value = { "classpath:config.properties" }, encoding = "utf-8")导入自定义得配置文件,用UTF-8编码。

注意,该属性类的每个属性必须有setter方法,不然属性会注入失败!!!!!

那么属性名name、country是否要和properties中的文件一致呢?

松配置

现在把config.properties修改如下:

#config.properties
people.name=张飞
people.country_name=蜀国

重新启动服务,也成功了,说明用@ConfigurationProperties标明的属性类中的属性名不必和properties文件中的属性名完全一样,这是因为@ConfigurationProperties支持Relaxed Binding,下面配置都是可以的,具体可以看看springboot开发文档

PropertyNote
acme.my-project.person.first-nameKebab case, which is recommended for use in .properties and .yml files.
acme.myProject.person.firstNameStandard camel case syntax.
acme.my_project.person.first_nameUnderscore notation, which is an alternative format for use in .properties and .yml files.
ACME_MYPROJECT_PERSON_FIRSTNAMEUpper case format, which is recommended when using system environment variables.

嵌套属性

@Component
@ConfigurationProperties(prefix = "people")
@PropertySource(value = { "classpath:config.properties" }, encoding = "utf-8")
public class PropertyConfig {

	private String name;

	private String countryName;

	private Salary salary;

	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public String getCountryName() {
		return countryName;
	}

	public void setCountryName(final String countryName) {
		this.countryName = countryName;
	}

	public Salary getSalary() {
		return salary;
	}

	public void setSalary(final Salary salary) {
		this.salary = salary;
	}

	public static class Salary {
		private int amount;
		private String unit;

		public int getAmount() {
			return amount;
		}

		public void setAmount(final int amount) {
			this.amount = amount;
		}

		public String getUnit() {
			return unit;
		}

		public void setUnit(final String unit) {
			this.unit = unit;
		}
	}
}
#config.properties
people.name=张飞
people.country_name=蜀国
people.salary.amount=2000
people.salary.unit=美元

同样,salary作为嵌套属性,也必须有setter方法。如果是下面情形,可以省略setter方法

@Component
@ConfigurationProperties(prefix = "people")
@PropertySource(value = { "classpath:config.properties" }, encoding = "utf-8")
public class PropertyConfig {

	private String name;

	private String countryName;

	private Salary salary = new Salary();//这里直接初始化

	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public String getCountryName() {
		return countryName;
	}

	public void setCountryName(final String countryName) {
		this.countryName = countryName;
	}

	public Salary getSalary() {
		return salary;
	}

	public static class Salary {
		private int amount;
		private String unit;

		public int getAmount() {
			return amount;
		}

		public void setAmount(final int amount) {
			this.amount = amount;
		}

		public String getUnit() {
			return unit;
		}

		public void setUnit(final String unit) {
			this.unit = unit;
		}
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值