《深刻淺出springboot2.x》讀書筆記

最近利用點閒散時間看了《深刻淺出springboot2.x》這本書,用了好久的springboot了,也看看有沒有沒注意過的細節。java

1.導圖

2.備忘

2.1 自定義配置優先級

事實上, SpringBoot的參數配置除了使用 properties文件以外,還能夠使用 yml文件等,它會以 下列的優先級順序進行加載 :spring

  • 命令行參數
  • 來自 java:comp/env 的丹IDI 屬性
  • Java 系統屬性( System.getProperties())
  • 操做系統環境變 量
  • RandomValuePrope1tySource 配置 的 random.*屬性值
  • jar包外部的 application-{profile}.properties 或 application.yml (帶 spring.profile)配置文件
  • jar包內部的 application-{profile}.properties 或 application.yml (帶 spring.profile)配置文件
  • jar包外部的 application.properties 或 application.yml (不帶 spring.profile)配置文件
  • jar包內部的 application.properties 或 application.ym (不帶 spring.profile)配置文件
  • @Configuration 註解類上的@PropertySource
  • 經過 SpringApplication.setDefaultProperties指定的默認屬性

2.2 @Autowired註解

  • 默認byType注入,首先根據類型找對應的Bean,若是對應的類型Bean不是惟一的,那麼根據屬性名稱和Bean名稱進行匹配,若是匹配得上,就使用,不然,拋出異常。數據庫

    public class Dog implements Animal {   
    }
    
    public class Cat implements Animal {
    }
    
    @Autowire
    private Animal dog = null; //兩個Animal的實現類,則根據名稱dog注入Dog類
    
    複製代碼
  • @Autowire(require=false) 容許找不到注入類,找不到時不拋出異常apache

  • 同類型狀況下,@Primary註解的類會被優先注入springboot

  • @Qualifier("dog")註解能夠顯示的指明注入類的名字bash

2.3 生命週期

2.4 做用域

@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Scope(WebApplicationContext.SCOPE_REQUEST)
@Scope(WebApplicationContext.SCOPE_SESSION)
@Scope(WebApplicationContext.SCOPE_APPLICATION)
複製代碼

2.5 AOP

  • 通知: 就是咱們編寫的但願Aop時執行的那個方法。咱們經過Aop但願咱們編寫的方法在目標方法執行前執行,或者執行後執行。
  • 切點:切點就是咱們咱們配置的知足咱們條件的目標方法。好比咱們規定:名字前面是select開頭的才執行咱們自定義的通知方法。那麼這些select開頭的方法就是切點。
  • 鏈接點:鏈接點能夠說是切點的全集。切點是鏈接點的子集。也能夠理解爲,鏈接點是咱們沒有定義那個select開頭規則時,知足條件的所有的方法。
  • 切面:切面是切點和通知的組合稱謂,就是變相給組合起了個名字。
@Aspect //切面
@Component
public class MyAspect {
    @Pointcut("execution(* com.fufu.demo.aop.HelloServiceImpl.sayHello(..))") //切點
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before() { 
        // 前置通知
        System.out.printf("MyAspect Before...");
    }
    
    @Around("pointCut()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { 
        // 環繞通知
        System.out.println("around before...");
        proceedingJoinPoint.proceed();
        System.out.println();
    }
}
複製代碼
  • @EnableAspectJAutoProxy

在完成了引入AOP依賴包後,通常來講並不須要去作其餘配置。也許在Spring中使用過註解配置方式的人會問是否須要在程序主類中增長@EnableAspectJAutoProxy來啓用,實際並不須要。mybatis

能夠看下面關於AOP的默認配置屬性,其中spring.aop.auto屬性默認是開啓的,也就是說只要引入了AOP依賴後,默認已經增長了@EnableAspectJAutoProxyapp

# AOP
spring.aop.auto=true # Add @EnableAspectJAutoProxy.
spring.aop.proxy-target-class=false # Whether subclass-based (CGLIB) proxies are to be created (true) as opposed to standard Java interface-based proxies (false).
複製代碼

而當咱們須要使用CGLIB來實現AOP的時候,須要配置spring.aop.proxy-target-class=true,否則默認使用的是標準Java的實現。dom

  • @Order

@Order(1)spring-boot

@Order(2)

@Order(3)

使用@Order註解能夠指定切面的執行順序,須要注意的是,對於前置通知都是從小到大運行的,對於後置通知和返回通知都是從大到小運行的,這是典型的責任鏈模式的順序。

2.6 數據庫

  • 數據源配置
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
複製代碼

Springboot 2.0 默認數據源爲,com.zaxxer.hikari.HikariDataSource

  • mybatis

嚴格來講, Spring 項目自己的項目是不支持 MyBatis 的, 那是由於 Spring 3 在 即將發 布版本時 ,MyBatis 3 尚未發佈正式版本,因此 Spring 的項目中都沒有考慮MyBatis 的整合。可是 MyBatis 社區爲了整合 Spring 本身開發了相應的開發包 ,所以在 Spring Boot中,咱們能夠依賴 MyBatis 社區提供的 starter。

<dependency>
	<groupid>org.mybatis.spring.boot</groupid>
	<artifactid>mybatis-spring-boot-starter</artifactid>
	<version>l.3.1</version>
</dependency>
複製代碼

從包名能夠看到, mybatis-spring-boot-starter是由 MyBatis社區開發的。

相關文章
相關標籤/搜索