最近利用点闲散时间看了《深刻浅出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社区开发的。