SpringBoot自定义配置


SpringBoot可以识别yml文件与properties文件,自定义配置最好使用properties格式,位于src/main/resource目录下的application.properties配置文件会被SpringBoot自动加载

@Value("${property}")获取配置属性

在application.properties配置文件中添加nikename=admin属性
在这里插入图片描述
通过@Value("${property}")注解获取配置nikename属性值

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserControll {
	
	@Value("${nikename}")
	private String nikename;
	
	@GetMapping("getUser")
	public String getUserName(){
		return nikename;
	}
	
}

在这里插入图片描述

通过将配置属性注入到JavaBean中,并通过JavaBean获取自定义属性

添加自定义属性

book.bookName=A Brief History Of Time
book.author=Stephen Hawkings

在这里插入图片描述
新建一个配置类SystemConfiguration.java通过@EnableConfigurationProperties(BookProperties.class)注解把使用 @ConfigurationProperties 的类进行注入

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(BookProperties.class)
public class SystemConfiguration {
	
}

新建一个book属性的javabean映射类

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix="book")
public class BookProperties {
	
	private String bookName;
	
	private String author;

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

}

WEB访问

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserControll {
	
	@Autowired
	private BookProperties bookProperties;
	
	@GetMapping("getUser")
	public String getUserName(){
		return bookProperties.getBookName()+" - by "+bookProperties.getAuthor();
	}
	
}

在这里插入图片描述

使用自定义配置文件将配置属性注入到JavaBean中,并通过JavaBean获取自定义属性

新建user.properties配置文件

system.user.name=zhangsan
system.user.sex=man
system.user.age=18

在这里插入图片描述
通过@PropertySource(“classpath:user.properties”)加载指定位置的properties文件

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="system.user")
@PropertySource("classpath:user.properties")
public class UserProperties {

	private String name;
	
	private String sex;
	
	private String age;

	public String getName() {
		return name;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}
	
}

WEB访问

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserControll {
	
	@Autowired
	private UserProperties userProperties;
	
	@GetMapping("getUser")
	public String getUserName(){
		return "name:"+userProperties.getName()+", age:"+userProperties.getAge()+", sex:"+userProperties.getSex();
	}
	
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值