SpringBoot 自定义启动器
作者:互联网
这里以 数据库连接池的启动器为例,当用户引入连接池的启动器之后,项目中就已经自动配置了连接池
1.具体实现步骤
- 创建一个启动器项目(我们需要将这个打包成一个依赖,供其他项目使用)
- 给启动器添加需要的依赖
- 创建属性配置类()
- 创建自动配置类()
- 编写自动配置文件(spring.factories)
- 使用自定义的启动器
2.具体实现
1.创建项目,添加依赖
创建一个项目 spring-boot-jdbc-starter
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu</groupId>
<artifactId>spring-boot-jdbc-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入spring‐boot‐starter;所有starter的基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--自动配置连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
2.创建属性配置类
@Component
@ConfigurationProperties(prefix = "spring.jdbc.datasource")
public class DataSourceProperties {
private String driverClassName;
private String url;
private String username;
private String password;
//getters, setters and toString...
}
3.创建自动配置类
// 这里我们没有添加条件注解,其实我们可以添加条件注解使这个配置类在一定条件下生效
@SpringBootConfiguration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceAutoConfiguration {
@Autowired
private DataSourceProperties dataSourceProperties;
@Bean
// 只有当 spring.jdbc.datasource.type=druid 的时候这个函数才生效
@ConditionalOnProperty(value = "spring.jdbc.datasource.type", havingValue = "druid")
public DataSource createDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
dataSource.setUrl(dataSourceProperties.getUrl());
dataSource.setUsername(dataSourceProperties.getUsername());
dataSource.setPassword(dataSourceProperties.getPassword());
return dataSource;
}
@Bean
// 只有当 spring.jdbc.datasource.type=c3p0 的时候这个函数才生效
@ConditionalOnProperty(value = "spring.jdbc.datasource.type",havingValue = "c3p0")
public DataSource createC3P0DataSource() throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(dataSourceProperties.getDriverClassName());
dataSource.setJdbcUrl(dataSourceProperties.getUrl());
dataSource.setUser(dataSourceProperties.getUsername());
dataSource.setPassword(dataSourceProperties.getPassword());
return dataSource;
}
}
4.编写 spring.factories 文件
文件必须建在resources/MATE-INF/spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.atguigu.autoconfig.DataSourceAutoConfiguration
做完了之后注意要执行 install , 安装项目,这样我们只要在其他项目中引入这个启动器就可以帮我们自动配置数据库连接池了
5.使用自定义启动器
我们只要在其他项目中正常引入这个自动器,并配置默认属性就可以了
引入依赖
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>spring-boot-jdbc-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
配置默认属性
spring:
jdbc:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql///business
username: root
password: root
type: c3p0
使用数据库链接池
@RestController
public class HelloController {
@Autowired
private DataSource dataSource;
@RequestMapping("/hello")
public String hello(String name){
System.out.println(dataSource.getClass());
return "Hello SpringBoot!!!" + name;
}
}
标签:jdbc,启动器,SpringBoot,自定义,dataSourceProperties,spring,boot,dataSource 来源: https://blog.csdn.net/qq_44704799/article/details/121178415