夥計,來跟我一塊兒學SpringBoot! 【第二彈】

你們好,我是小菜,一個渴望在互聯網行業作到蔡不菜的小菜。可柔可剛,點贊則柔,白嫖則剛!
死鬼~看完記得給我來個三連哦!java

本文主要介紹 SprinBoot
若有須要,能夠參考
若有幫助,不忘 點贊mysql

1、 配置嵌入式Servlet容器

1)定製和修改Servlet容器的相關配置

法1:修改和server有關的配置web

properties server.tomcat.uri-encoding=UTF-8 //通用的Servlet容器設置 server.xxx //Tomcat的設置 server.tomcat.xxx spring

法2:編寫一個EmbeddedServletContainerCustomizer:嵌入式的Servlet容器的定製器;來修改Servlet容器的配置
sql

2)註冊Servlet三大組件

  • Servlet
  • Filter
  • Listener

因爲 SpringBoot 默認是以jar包的方式啓動嵌入式的Servlet容器來啓動SpringBoot的web應用,因此沒有web.xml文件數據庫

註冊三大組件用如下方式apache

ServletRegistrationBean
tomcat


FilterRegistrationBean

ServletListenerRegistrationBean

3)替換爲其餘嵌入式Servlet容器

默認支持如下容器springboot

  • Tomcat
<!-- 引入web模塊默認就是使用嵌入式的Tomcat做爲Servlet容器 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
複製代碼
  • Jetty
<!-- 先排除內置默認容器 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
      <exclusion>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <groupId>org.springframework.boot</groupId>
      </exclusion>
   </exclusions>
</dependency>

<!--引入其餘的Servlet容器-->
<dependency>
   <artifactId>spring-boot-starter-jetty</artifactId>
   <groupId>org.springframework.boot</groupId>
</dependency>
複製代碼
  • Undertow
<!-- 先排除內置默認容器 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
      <exclusion>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <groupId>org.springframework.boot</groupId>
      </exclusion>
   </exclusions>
</dependency>

<!--引入其餘的Servlet容器-->
<dependency>
   <artifactId>spring-boot-starter-undertow</artifactId>
   <groupId>org.springframework.boot</groupId>
</dependency>
複製代碼

4)嵌入式Servlet容器自動配置原理

EmbeddedServletContainerAutoConfiguration
服務器

  • EmbeddedServletContainerFactory(嵌入式Servlet容器工廠)

    public interface EmbeddedServletContainerFactory {
     //獲取嵌入式的Servlet容器
     EmbeddedServletContainer getEmbeddedServletContainer(
           ServletContextInitializer... initializers)
    ;
      }
    }
    複製代碼

  • EmbeddedServletContainer:(嵌入式的Servlet容器)

  • TomcatEmbeddedServletContainerFactory爲例

  • 嵌入式容器的配置修改怎麼生效

  • 方法1ServerProperties

  • 方法2EmbeddedServletContainerCustomizer(定製器幫咱們修改了Servlet容器的配置)

修改原理

容器中導入了EmbeddedServletContainerCustomizerBeanPostProcessor

ServerProperties:也是定製器

  • SpringBoot根據導入的依賴狀況,給容器中添加相應的EmbeddedServletContainerFactory【TomcatEmbeddedServletContainerFactory】
  • 容器中某個組件要建立對象就會驚動後置處理器:EmbeddedServletContainerCustomizerBeanPostProcessor(只要是嵌入式的Servlet容器工廠,後置處理器就工做)
  • 後置處理器,從容器中獲取全部的EmbeddedServletContainerCustomizer,調用定製器的定製方法

5)使用外置的Servlet容器

嵌入式Servlet容器:應用打成可執行的jar
優勢: 簡單、便攜
缺點:默認不支持JSP、優化定製比較複雜

步驟

  1. 建立一個war項目

  2. 將嵌入式的Tomcat指定爲provided

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>
    複製代碼
  3. 編寫一個SpringBootServletInitializer的子類,並調用configure()方法

    public class ServletInitializer extends SpringBootServletInitializer {
       @Override
       protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
           //傳入SpringBoot應用的主程序
          return application.sources(SpringBoot04WebJspApplication.class);
       }
    }
    複製代碼
  4. 啓動服務器就可使用

原理

jar包:執行SpringBoot主類的main方法,啓動 Ioc容器,建立嵌入式的Servlet容器

war包:啓動服務器,服務器啓動SpringBoot應用SpringBootServletInitializer,啓動 Ioc容器

2、數據訪問

對於數據訪問層,不管是SQL仍是NOSQLSpring Boot默認採用整合Spring Data的方式進行統一處理,添加大量自動配置,屏蔽了不少設置。引入各類xxxTemplatexxxRepository來簡化咱們對數據訪問層的操做。對咱們來講只須要進行簡單的設置便可。

1)整合基本JDBC與數據源

  • 引入starter :spring-boot-starter-jdbc
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
複製代碼
  • 配置application.yml
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://192.168.15.22:3306/test
    driver-class-name: com.mysql.jdbc.Driver
複製代碼

結論

  • 默認使用org.apache.tomcat.jdbc.pool.DataSource做爲數據源
  • 數據源的相關配置都在DataSourceProperties裏面

自動配置原理

  1. 參考DataSourceConfiguration,根據配置建立數據源,默認使用Tomcat鏈接池。可使用spring.datasource.type指定自定義的數據源類型

  2. SpringBoot默承認以支持:

  • org.apache.tomcat.jdbc.pool.DataSource
  • HikariDataSource
  • BasicDataSource
  1. 自定義數據源類型

    @ConditionalOnMissingBean(DataSource.class)
    @ConditionalOnProperty(name = "spring.datasource.type")
    static class Generic {
      @Bean
      public DataSource dataSource(DataSourceProperties properties) {
          //使用DataSourceBuilder建立數據源,利用反射建立響應type的數據源,而且綁定相關屬性
         return properties.initializeDataSourceBuilder().build();
      }
    }
    複製代碼
  2. DataSourceInitializer:ApplicationListener

    做用

  3. runSchemaScripts():運行建表語句

  4. runDataScripts():運行插入數據的sql語句

    默認只須要將文件命名爲

    schema-*.sqldata-*.sql

  5. 操做數據庫:自動配置了JdbcTemplate操做數據庫

2)整合Druid數據源

  • 引入druid數據源
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.8</version>
</dependency>
複製代碼
  • 配置文件
  • 配置數據源

3)整合MyBatis

  • 引入依賴:
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>
複製代碼

步驟
1)配置數據源相關屬性
2)給數據庫建表
3)建立JavaBean
4)註解使用


自定義MyBatis的配置規則

  • 在容器中添加一個ConfigurationCustomizer

  • 在啓動類中添加MapperScan註解批量掃描全部的Mapper接口

5)配置文件使用

mybatis:
  #指定全局配置文件的位置
  config-location: classpath:mybatis/mybatis-config.xml
  #指定sql映射文件的位置
  mapper-locations: classpath:mybatis/mapper/*.xml 
複製代碼

4)整合SpringData JPA

SpringData簡介

Spring Data是一個用於簡化數據庫訪問,並支持雲服務的開源框架。其主要目標是使得對數據的訪問變得方便快捷。
它能夠極大的簡化JPA的寫法,能夠在幾乎不用寫實現的狀況下,實現對數據的訪問和操做。除了CRUD外,還包括如分頁、排序等一些經常使用的功能。


SpringData整合

  • 編寫一個實體類(bean)和數據表進行映射,而且配置好映射關係
  • 編寫一個Dao接口來操做實體類對應的數據表(Repository)
  • 配置JpaProperties
spring:  
 jpa:
    hibernate:
      #更新或者建立數據表結構
      ddl-auto: update
      #控制檯顯示SQL
         show-sql: true
複製代碼

5)事件監聽機制

如下文件是配置在META-INF/spring.factories

  • ApplicationContextInitializer
  • SpringApplicationRunListener

以上兩個須要配置在(META-INF/spring.factories

org.springframework.context.ApplicationContextInitializer=\
com.atguigu.springboot.listener.HelloApplicationContextInitializer

org.springframework.boot.SpringApplicationRunListener=\
com.atguigu.springboot.listener.HelloSpringApplicationRunListener
複製代碼

如下兩個只須要放在ioc容器中

  • ApplicationRunner
  • CommandLineRunner
看完不讚,都是壞蛋
看完不讚,都是壞蛋

今天的你多努力一點,明天的你就能少說一句求人的話!

我是小菜,一個和你一塊兒學習的男人。 💋

相關文章
相關標籤/搜索