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();
}
}

本文详细介绍了SpringBoot中配置属性的三种方式:使用@Value注解直接获取配置、通过JavaBean映射自定义配置文件、以及使用@ConfigurationProperties注解配合自定义配置文件。通过实例演示了如何在application.properties中添加属性并注入到Controller中,如何创建自定义的JavaBean来映射配置文件中的属性,以及如何使用@PropertySource加载外部配置文件。
1114

被折叠的 条评论
为什么被折叠?



