spring bootConfigurationProperties 配置
application.properties文件
server.port=8888
server.servlet.context-path=/HelloWorld
helloWorld=spring boot 你好
mysql.jdbcName=com.mysql.jdbc.Driver
mysql.dbUrl=jdbc:mysql://localhost:3306/db_boot
mysql.userName=root
mysql.password=123456
Mysql属性配置文件
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Mysql属性配置文件
* @author Administrator
*
*/
@Component
@ConfigurationProperties(prefix="mysql")
public class MysqlProperties {
private String jdbcName;
private String dbUrl;
private String userName;
private String password;
public String getJdbcName() {
return jdbcName;
}
public void setJdbcName(String jdbcName) {
this.jdbcName = jdbcName;
}
public String getDbUrl() {
return dbUrl;
}
public void setDbUrl(String dbUrl) {
this.dbUrl = dbUrl;
}
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 HelloWorld1Controller {
@Value("${helloWorld}")
private String helloWorld;
@Resource
private MysqlProperties mysqlProperties;
@RequestMapping("/helloWorld")
public String say(){
return helloWorld;
}
@RequestMapping("/showJdbc")
public String showJdbc(){
return "mysql.jdbcName:"+mysqlProperties.getJdbcName()+"<br/>"
+"mysql.dbUrl:"+mysqlProperties.getDbUrl()+"<br/>"
+"mysql.userName:"+mysqlProperties.getUserName()+"<br/>"
+"mysql.password:"+mysqlProperties.getPassword()+"<br/>";
}
}
运行结果