@ConfigurationProperties
在aplication.properties 中添加如下一段配置:
mysql.jdbcName=com.mysql.jdbc.Driver
mysql.dbUrl=jdbc:mysql://localhost:3306/db_boot
mysql.userName=root
mysql.password=123456
将这一段配置数据注入如下Bean中:
/**
属性配置文件
@Component
@ConfigurationProperties(prefix="mysql")
public class MysqlProperties {
省略getter/setter
}
1.@ConfigurationProperties中的prefix属性描述了要加载的配置文件的前缀。
2.如果配直文件是一个YAML文件,那么可以将数据注入一个集合中。
3. Spring Boot采用了一种宽松的规则来进行属性绑定,如果Bean中的属性名为authorName,那么配直文件中的属性可以是book.author-name、book.author-name、book.authorName。
@Value
在aplication.properties 中添加如下一段配置:
helloWorld=spring boot
使用@Value注解:
@RestController
public class HelloWorldController {
}