第一节:项目内置属性
application.properties配置整个项目的,相当于以前的web.xml;
注意到上一节的访问HelloWorld时,项目路径也没有加;直接是http://localhost:8080/helloWorld;
因为它默认的server.servlet.context-path=/
修改如下:
src/main/resource/application.properties:
server.port=8888server.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=8888server.servlet.context-path=/HelloWorldhelloWorld=spring boot hello!mysql.jdbcName=com.mysql.jdbc.Drivermysql.dbUrl=jdbc:mysql://localhost:3306/db_rootmysql.userName=rootmysql.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;@RestControllerpublic 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+"" +"mysql.dbUrl:"+dbUrl+"" +"mysql.userName:"+userName+"" +"mysql.password:"+password+""; }}
浏览器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_rootmysql.userName:rootmysql.password:123456
第三节:ConfigurationProperties 配置
上面自定义属性,如果写了很多呢,在多个地方用到,那么还一个一个的写,@Value("${key}"),就很麻烦了。
使用封装。
com.cy.properties.MysqlProperties.java:
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;@RestControllerpublic class HelloWorldController { @Resource private MysqlProperties mysqlProperties; @Value("${helloWorld}") private String helloWorld; @RequestMapping("/helloWorld") public String say(){ return helloWorld; } @RequestMapping("/showJdbc") public String showJdbc(){ return "mysql.jdbcName:"+mysqlProperties.getJdbcName()+"" +"mysql.dbUrl:"+mysqlProperties.getDbUrl()+"" +"mysql.userName:"+mysqlProperties.getUserName()+"" +"mysql.password:"+mysqlProperties.getPassword()+""; }}
浏览器http://localhost:8888/HelloWorld/showJdbc,显示之前一样;