最近利用點閒散時間看了《深刻淺出springboot2.x》這本書,用了好久的springboot了,也看看有沒有沒注意過的細節。java
事實上, SpringBoot的參數配置除了使用 properties文件以外,還能夠使用 yml文件等,它會以 下列的優先級順序進行加載 :spring
默認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
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Scope(WebApplicationContext.SCOPE_REQUEST)
@Scope(WebApplicationContext.SCOPE_SESSION)
@Scope(WebApplicationContext.SCOPE_APPLICATION)
複製代碼
@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();
}
}
複製代碼
在完成了引入AOP依賴包後,通常來講並不須要去作其餘配置。也許在Spring中使用過註解配置方式的人會問是否須要在程序主類中增長@EnableAspectJAutoProxy
來啓用,實際並不須要。mybatis
能夠看下面關於AOP的默認配置屬性,其中spring.aop.auto
屬性默認是開啓的,也就是說只要引入了AOP依賴後,默認已經增長了@EnableAspectJAutoProxy
。app
# 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(1)spring-boot
@Order(2)
@Order(3)
使用@Order註解能夠指定切面的執行順序,須要注意的是,對於前置通知都是從小到大運行的,對於後置通知和返回通知都是從大到小運行的,這是典型的責任鏈模式的順序。
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
複製代碼
Springboot 2.0 默認數據源爲,com.zaxxer.hikari.HikariDataSource
嚴格來講, 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社區開發的。