javascript
spring boot学习(2) SpringBoot 项目属性配置
第一節:項目內置屬性
application.properties配置整個項目的,相當于以前的web.xml; 注意到上一節的訪問HelloWorld時,項目路徑也沒有加;直接是http://localhost:8080/helloWorld; 因為它默認的server.servlet.context-path=/ 修改如下: src/main/resource/application.properties: 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=123456com.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
?
第三節: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;@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,顯示之前一樣;
轉載于:https://www.cnblogs.com/tenWood/p/8641387.html
總結
以上是生活随笔為你收集整理的spring boot学习(2) SpringBoot 项目属性配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: textarea标签内的文字无缘故居中解
- 下一篇: python D28 粘包