package com.example.springboots;
import javax.sql.DataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.springboots.dao")
/**
* 1)、Spring boot的CommandLineRunner接口主要用于实现在应用初始化后,
* 去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次。
* 2)、用@Order注解去设置多个CommandLineRunner实现类的执行顺序
* @author Jam
*
*/
public class SpringbootsApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringbootsApplication.class, args);
}
@Autowired
DataSource dataSource;
/**
* 重写CommandLineRunner接口的run方法,将在程序启动初始化之后运行
*/
@Override
public void run(String... args) throws Exception {
System.out.println("DATASOURCE = " + dataSource);
}
}
2019-05-06 11:34:04.287 INFO 5752 --- [ restartedMain] c.e.springboots.SpringbootsApplication : Started SpringbootsApplication in 2.026 seconds (JVM running for 1230.426)
DATASOURCE = HikariDataSource (null)
2019-05-06 11:34:04.287 INFO 5752 --- [ restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation delta:
HikariDataSource表示当前使用的是HikariDataSource数据库连接池
引入相关依赖,在pom.xml引入
com.alibaba
druid
1.1.14
在application.properties配置文件中添加
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
重新运行启动类,获得日志:
2019-05-06 11:39:39.170 INFO 5752 --- [ restartedMain] c.e.springboots.SpringbootsApplication : Started SpringbootsApplication in 2.448 seconds (JVM running for 1565.299)
DATASOURCE = {
CreateTime:"2019-05-06 11:39:38",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
2019-05-06 11:39:39.177 INFO 5752 --- [ restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation delta:
可以对比,数据库连接池已经成功被修改为Druid!!!