在配置application.yml文件中使用自定义属性

引入springboot依赖

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

自定义属性

在application.properties中配置自定义属性。

top.guoj.user-name=hello
top.guoj.pass-word=word

## OR
top.guoj.userName=hello
top.guoj.passWord=word

新建映射类

通过@ConfigurationProperties注解并配置prefix属性。

@Component
@ConfigurationProperties(prefix = "top.guoj")
public class Configure {

	private String userName;
	private String passWord;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
}

使用

@RestController
public class HelloController {

	@Autowired
	private Configure configure;

	@RequestMapping("/configure")
	String configure() {
		return configure.getUserName() + configure.getPassWord();
	}
}