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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring Boot2.x-03Spring Boot基础

發布時間:2025/3/21 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot2.x-03Spring Boot基础 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 搭建Spring Boot開發環境
    • 使用Spring Tool Suit 構建
    • 使用 IntelliJ IDEA來構建
  • Spring Boot配置
    • 入口類的main方法和@SpringBootApplication注解
    • 全局配置文件application.properties或者application.yml
    • 使用@ImportResource加載xml
  • 屬性配置
    • 常規屬性配置
    • 基于properties的類型安全的配置
  • 開發、測試、生產環境配置通過Profile來區分
    • 配置
    • 在application.properties中指定使用的文件
  • 代碼

搭建Spring Boot開發環境

搭建方式有很多種,這里不一一列出了,僅列出如下兩種常用的方式。

使用Spring Tool Suit 構建

Spring Boot-Spring Tool Suit + Gradle 構建第一個Spring Boot 項目01

Spring Boot-Spring Tool Suit + Gradle 構建第一個Spring Boot 項目02


使用 IntelliJ IDEA來構建

版本: IntelliJ IDEA 2018.2.5 Ultimate 版本 商用版本

Spring Boot 2.0.6

跟隨提示,一步步的操作即可 ,依賴的話,初次構建只要選擇Web即可滿足。


如果沒有Spring Initializr ,需要安裝插件

操作:setting—>plugins—>搜索Spring boot—>勾選安裝Spring boot插件,重啟IDEA。


Spring Boot配置

入口類的main方法和@SpringBootApplication注解

在自動生成的工程里面,我們來看下應用啟動的入口類,類的名稱取決于你的工程的名稱,比如這里我的是SpringbootmasterApplication

package com.artisan.springbootmaster;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class SpringbootmasterApplication {public static void main(String[] args) {SpringApplication.run(SpringbootmasterApplication.class, args);} }

看下@SpringBootApplication注解

  • @SpringBootConfiguration繼承自@Configuration,標注當前類是配置類,并會將當前類內聲明的一個或多個以@Bean注解標記的方法的實例納入到spring容器中,Bean的實例名就是方法名。

  • @EnableAutoConfiguration 自動配置,依賴AutoConfigurationImportSelector。具體的細節以后再說。


全局配置文件application.properties或者application.yml

Spring Boot 不僅支持常規的Properties配置文件,還支持yaml語言的配置文件。 這里我們用Properties作為配置文件。

我們以修改端口為例子,講Tomcat默認的8080 改為8088端口啟動。

application.properties 增加

server.port=8088

可配置的屬性server開頭的屬性見 org.springframework.boot.autoconfigure.web.ServerProperties

啟動驗證

如果使用application.yml配置,如下

server:port: 8080

結果是相同的。


使用@ImportResource加載xml

雖然Spring boot提倡使用Java注解的方式來實現零配置的應用開發,但是并不代表不支持加載xml配置。

Spring提供了@ImportResource來加載xml文件。

用法也是很簡單,這里就不貼例子了。


屬性配置

常規屬性配置

在Spring Boot 中,只需要在application.properties中定義屬性,直接使用@Value注入即可

示例:
在application.properties增加兩個屬性

artisan.name=spring boot artisan.age=99

獲取

package com.artisan.springbootmaster.controller;import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;import java.util.HashMap; import java.util.Map;@RestController public class ArtisanController {@Value("${artisan.name}")private String name;@Value("${artisan.age}")private int age;@GetMapping("/loadPropertiesValue")public String loadPropertiesValue(){return "artisan name:【" + name + "】 , age:【" + age + "】";}}

訪問:http://localhost:8080/loadPropertiesValue


基于properties的類型安全的配置

上述這種方式,如果屬性很多,需要一個個的使用@Value注入,顯得十分的麻煩。 Spring Boot提供了基于類型安全的配置方式,可以使用@ConfigurationProperties將Properties的屬性和一個Bean及其屬性關聯,從而實現類型安全的配置

示例:

假設有個配置文件 artisan.properties

xxx.name=artisan_self xxx.age=23

配置類

package com.artisan.springbootmaster.conf;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;@Component @ConfigurationProperties(prefix = "xxx") @PropertySource(value = "classpath:artisan.properties") public class ArtisanConfig {private String name;private int age ;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;} }

prefix 要和 配置文件的前綴保持一致。 屬性名稱也要和配置文件中的一致, 否則獲取不到該值。

如果需要指定特定位置的配置文件,可以使用@PropertySource注解。舊版本@ConfigurationProperties中的locations已經被廢棄了。

同時,不要忘記給該類加上@Component,使其成為Spring管理的bean


使用: 直接 @Autowired注入該類即可獲取

package com.artisan.springbootmaster.controller;import com.artisan.springbootmaster.conf.ArtisanConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;import java.util.HashMap; import java.util.Map;@RestController public class ArtisanConfigLoadTest {@Autowiredprivate ArtisanConfig artisanConfig;@GetMapping("/loadSelfConfigProperties")public Map<String,Object> loadConf(){Map<String,Object> map = new HashMap<>();map.put("name",artisanConfig.getName());map.put("value", artisanConfig.getAge());return map;} }

啟動測試:


開發、測試、生產環境配置通過Profile來區分

Profile是Spring對不同環境提供不同配置功能的支持,可以通過指定參數等方式快速切換環境。默認使用application.properties配置文件

格式

application-{profile}.properties

配置

簡單起見,我們僅僅指定端口

application-dev.properties

server.port=8888

application-test.properties

server.port=9999

application-prod.properties

server.port=80

在application.properties中指定使用的文件

這里我們指定prod ,即啟動80端口

#spring.profiles.active=dev #spring.profiles.active=test spring.profiles.active=prod

觀察日志:


代碼

https://github.com/yangshangwei/springbootmaster

總結

以上是生活随笔為你收集整理的Spring Boot2.x-03Spring Boot基础的全部內容,希望文章能夠幫你解決所遇到的問題。

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