第一节:项目内置属性
server.port=8888
server.servlet.context-path=/HelloWorld
重新启动,输入http://localhost:8888/HelloWorld/helloWorld,页面显示spring boot你好;
1.port端口变成了8888;
2.项目根路径变了,context-path是/HelloWorld了;
第二节:自定义属性
可以在application.properties中配置一些自定义属性:xx.xx也行:
使用@Value("${key}")来注入到属性值中;
server.port=8888 server.servlet.context-path=/HelloWorldhelloWorld=spring boot hello!mysql.jdbcName=com.mysql.jdbc.Driver mysql.dbUrl=jdbc:mysql://localhost:3306/db_root mysql.userName=root mysql.password=123456
com.cy.controller.HelloWorldController.java:
package com.cy.controller;import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class HelloWorldController {@Value("${helloWorld}")private String helloWorld;@Value("${mysql.jdbcName}")private String jdbcName;@Value("${mysql.dbUrl}")private String dbUrl;@Value("${mysql.userName}")private String userName;@Value("${mysql.password}")private String password;@RequestMapping("/helloWorld")public String say(){return helloWorld;}@RequestMapping("/showJdbc")public String showJdbc(){return "mysql.jdbcName:"+jdbcName+"<br/>"+"mysql.dbUrl:"+dbUrl+"<br/>"+"mysql.userName:"+userName+"<br/>"+"mysql.password:"+password+"<br/>";} }
浏览器http://localhost:8888/HelloWorld/helloWorld,显示:spring boot hello!
浏览器http://localhost:8888/HelloWorld/showJdbc,显示:
mysql.jdbcName:com.mysql.jdbc.Driver
mysql.dbUrl:jdbc:mysql://localhost:3306/db_root
mysql.userName:root
mysql.password:123456
package com.cy.properties;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;/*** mysql属性配置文件* @author CY**/ @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;}}
HelloWorldController.java中使用它:
package com.cy.controller;import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import com.cy.properties.MysqlProperties;@RestController public class HelloWorldController {@Resourceprivate MysqlProperties mysqlProperties;@Value("${helloWorld}")private String helloWorld;@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/>";} }
浏览器http://localhost:8888/HelloWorld/showJdbc,显示之前一样;