日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

spring boot学习(2) SpringBoot 项目属性配置

發布時間:2024/7/19 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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=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

?

第三節: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 项目属性配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。