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

歡迎訪問 生活随笔!

生活随笔

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

javascript

idea springboot 无法run_2021 最新版 Spring Boot 速记教程

發布時間:2025/3/19 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 idea springboot 无法run_2021 最新版 Spring Boot 速记教程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

優質文章,及時送達

本文來源:http://r6d.cn/X6FP

結束了前面的《Spring 源碼深度學習》,八月給自己放松了一下,看了幾本小說和電視劇,還有寫一個工作中用到的小工具,周報數據渲染的前端界面(前端是真的難)。

當然技術上的學習也要注意,所以看了松哥寫的《Spring Boot + Vue 全棧開發》,來系統學習 SpringBoot,下面是簡單的速記,根據使用場景可以快速定位到知識點:

Demo腳手架項目地址:

https://github.com/Vip-Augus/springboot-note

Table of Contentsgenerated with DocToc

  • SpringBoot 速記

    • 一、引入依賴

    • 二、配置 Swagger 參數

    • 一、引入依賴

    • 二、配置郵箱的參數

    • 三、寫模板和發送內容

    • 一、引用 Redis 依賴

    • 二、參數配置

    • 三、代碼使用

    • 一、添加 mybatis 和 druid 依賴

    • 二、配置數據庫和連接池參數

    • 三、其他 mybatis 配置

    • @ExceptionHandler 錯誤處理

    • @ModelAttribute 視圖屬性

    • 常規配置

    • HTTPS 配置

    • 構建項目

    • SpringBoot 基礎配置

    • Spring Boot Starters

    • @SpringBootApplication

    • Web 容器配置

    • @ConfigurationProperties

    • Profile

    • @ControllerAdvice 用來處理全局數據

    • CORS 支持,跨域資源共享

    • 注冊 MVC 攔截器

    • 開啟 AOP 切面控制

    • 整合 Mybatis 和 Druid

    • 整合 Redis

    • 發送 HTML 樣式的郵件

    • 整合 Swagger (API 文檔)

    • 總結

    • 參考資料

構建項目

相比于使用 IDEA的模板創建項目,我更推薦的是在Spring官網上選擇參數一步生成項目。

https://start.spring.io/

關于 IDEA 發布過很多文字,可以關注微信公眾號 Java后端,關注后輸入 666 命令下載 Spring Boot 和 IDEA 相關文字的 PDF。

我們只需要做的事情,就是修改組織名和項目名,點擊 Generate the project,下載到本地,然后使用IDEA打開

這個時候,不需要任何配置,點擊 Application類的run方法就能直接啟動項目。

SpringBoot 基礎配置

Spring Boot Starters

引用自參考資料 1 描述:

starter的理念:starter 會把所有用到的依賴都給包含進來,避免了開發者自己去引入依賴所帶來的麻煩。需要注意的是不同的 starter 是為了解決不同的依賴,所以它們內部的實現可能會有很大的差異,例如 jpa 的 starter 和 Redis 的 starter 可能實現就不一樣,這是因為 starter 的本質在于 synthesize,這是一層在邏輯層面的抽象,也許這種理念有點類似于 Docker,因為它們都是在做一個“包裝”的操作,如果你知道 Docker 是為了解決什么問題的,也許你可以用 Docker 和 starter 做一個類比。

我們知道在 SpringBoot中很重要的一個概念就是,「約定優于配置」,通過特定方式的配置,可以減少很多步驟來實現想要的功能。

例如如果我們想要使用緩存 Redis

在之前的可能需要通過以下幾個步驟:

  • 在 pom文件引入特定版本的redis

  • 在 .properties文件中配置參數

  • 根據參數,新建一個又一個 jedis連接

  • 定義一個工具類,手動創建連接池來管理

  • 經歷了上面的步驟,我們才能正式使用 Redis

    但在 Spring Boot中,一切因為Starter變得簡單

  • 在 pom文件中引入spring-boot-starter-data-redis

  • 在 .properties文件中配置參數

  • 通過上面兩個步驟,配置自動生效,具體生效的 bean是RedisAutoConfiguration,自動配置類的名字都有一個特點,叫做xxxAutoConfiguration。

    可以來簡單看下這個類:

    @Configuration
    @ConditionalOnClass(RedisOperations.class)
    @EnableConfigurationProperties(RedisProperties.class)
    @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
    public class RedisAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory)
    throws UnknownHostException {
    RedisTemplate template = new RedisTemplate<>;
    template.setConnectionFactory(redisConnectionFactory);
    return template;
    }
    @Bean
    @ConditionalOnMissingBean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)
    throws UnknownHostException {
    StringRedisTemplate template = new StringRedisTemplate;
    template.setConnectionFactory(redisConnectionFactory);
    return template;
    }
    }
    @ConfigurationProperties(prefix = "spring.redis")
    public class RedisProperties {...}

    可以看到,Redis自動配置類,讀取了以spring.redis為前綴的配置,然后加載redisTemplate到容器中,然后我們在應用中就能使用RedisTemplate來對緩存進行操作~(還有很多細節沒有細說,例如@ConditionalOnMissingBean先留個坑(●′?`●)ノ)

    @Autowired
    private RedisTemplate redisTemplate;

    ValueOperations ops2 = redisTemplate.opsForValue;
    Book book = (Book) ops2.get("b1");

    @SpringBootApplication

    該注解是加載項目的啟動類上的,而且它是一個組合注解:

    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
    @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
    public @interface SpringBootApplication {...}

    下面是這三個核心注解的解釋:

    注解名解釋
    @SpringBootConfiguration表明這是一個配置類,開發者可以在這個類中配置 Bean
    @EnableAutoConfiguration表示開啟自動化配置
    @ComponentScan完成包掃描,默認掃描的類位于當前類所在包的下面

    通過該注解,我們執行 mian方法:

    SpringApplication.run(SpringBootLearnApplication.class, args);

    就可以啟動一個 SpringApplicaiton應用了。

    Web 容器配置

    常規配置

    配置名解釋
    server.port=8081配置了容器的端口號,默認是 8080
    server.error.path=/error配置了項目出錯時跳轉的頁面
    server.servlet.session.timeout=30msession 失效時間,m 表示分鐘,如果不寫單位,默認是秒 s
    server.servlet.context-path=/項目名稱,不配置時默認為/。配置后,訪問時需加上前綴
    server.tomcat.uri-encoding=utf-8Tomcat 請求編碼格式
    server.tomcat.max-threads=500Tomcat 最大線程數
    server.tomcat.basedir=/home/tmpTomcat 運行日志和臨時文件的目錄,如不配置,默認使用系統的臨時目錄

    HTTPS 配置

    配置名解釋
    server.ssl.key-store=xxx秘鑰文件名
    server.ssl.key-alias=xxx秘鑰別名
    server.ssl.key-store-password=123456秘鑰密碼

    想要詳細了解如何配置 HTTPS,可以參考這篇文章 Spring Boot 使用SSL-HTTPS

    @ConfigurationProperties

    這個注解可以放在類上或者 @Bean注解所在方法上,這樣SpringBoot就能夠從配置文件中,讀取特定前綴的配置,將屬性值注入到對應的屬性。

    使用例子:

    @Configuration
    @ConfigurationProperties(prefix = "spring.datasource")
    public class DruidConfigBean {

    private Integer initialSize;

    private Integer minIdle;

    private Integer maxActive;

    private List customs;
    ...
    }
    application.properties
    spring.datasource.initialSize=5
    spring.datasource.minIdle=5
    spring.datasource.maxActive=20
    spring.datasource.customs=test1,test2,test3

    其中,如果對象是列表結構,可以在配置文件中使用 , 逗號進行分割,然后注入到相應的屬性中。

    Profile

    使用該屬性,可以快速切換配置文件,在 SpringBoot默認約定中,不同環境下配置文件名稱規則為application-{profile}.propertie,profile占位符表示當前環境的名稱。

    1、配置 application.properties

    spring.profiles.active=dev

    2、在代碼中配置 在啟動類的 main方法上添加setAdditionalProfiles("{profile}");

    SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringBootLearnApplication.class);
    builder.application.setAdditionalProfiles("prod");
    builder.run(args);

    3、啟動參數配置

    java -jar demo.jar --spring.active.profile=dev

    @ControllerAdvice 用來處理全局數據

    @ControllerAdvice是@Controller的增強版。主要用來處理全局數據,一般搭配@ExceptionHandler、@ModelAttribute以及@InitBinder使用。

    @ExceptionHandler 錯誤處理

    /**
    * 加強版控制器,攔截自定義的異常處理
    *
    */
    @ControllerAdvice
    public class CustomExceptionHandler {

    // 指定全局攔截的異常類型,統一處理
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public void uploadException(MaxUploadSizeExceededException e, HttpServletResponse response) throws IOException {
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter;
    out.write("上傳文件大小超出限制");
    out.flush;
    out.close;
    }
    }

    @ModelAttribute 視圖屬性

    @ControllerAdvice
    public class CustomModelAttribute {

    //
    @ModelAttribute(value = "info")
    public Map userInfo throws IOException {
    Map map = new HashMap<>;
    map.put("test

    總結

    以上是生活随笔為你收集整理的idea springboot 无法run_2021 最新版 Spring Boot 速记教程的全部內容,希望文章能夠幫你解決所遇到的問題。

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