概述
这里将开发一个spring boot starter,它用于连接mysql并自动生成一个mysql数据库连接池的datasource。
一、环境
jdk:13
spring boot:2.1.0.RELEASE
mysql jdbc驱动使用mysql-connector-java
数据库连接池采用HikariCP
二、创建spring-boot-start-mysql工程
1.新建pom.xml文件
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<groupId>com.bry.springboot</groupId>
<artifactId>spring-boot-start-mysql</artifactId>
<version>2.1.0.RELEASE</version>
<name>spring-boot-start-mysql</name>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
</dependencies>
</project>
2.建立数据库连接属性类MysqlProperties.java
package com.bruce.mysqlstart;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "mysql")
public class MysqlProperties {
private String driverURL;
private String user;
private String password;
public String getDriverURL() {
return driverURL;
}
public void setDriverURL(String driverURL) {
this.driverURL = driverURL;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@ConfigurationProperties(prefix = “mysql”): 数据库配置以mysql开头,如 msyql.driverURL,msyql.driverURL,mysql.user,mysql.password
3.创建mysql连接池自动配置类MysqlAutoConfiguration.java
package com.bruce.mysqlstart;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@EnableConfigurationProperties(MysqlProperties.class)
@ConditionalOnClass({ com.zaxxer.hikari.HikariDataSource.class, com.mysql.jdbc.Driver.class })
public class MysqlAutoConfiguration {
@Autowired
MysqlProperties mysqlProperties;
@Bean(destroyMethod = "close")
@ConditionalOnMissingBean(DataSource.class)
public DataSource getPhoneService() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(mysqlProperties.getDriverURL());
hikariConfig.setUsername(mysqlProperties.getUser());
hikariConfig.setPassword(mysqlProperties.getPassword());
// In case we're using MySQL, these settings are recommended by HikariCP:
hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
hikariConfig.addDataSourceProperty("useServerPrepStmts", "true");
hikariConfig.setConnectionTestQuery("select CURRENT_DATE");
HikariDataSource hikariDataSource = new HikariDataSource(hikariConfig);
return hikariDataSource;
}
}
@EnableConfigurationProperties(MysqlProperties.class): 加载属性文件到MysqlProperitest.claa中
@ConditionalOnClass({ com.zaxxer.hikari.HikariDataSource.class, com.mysql.jdbc.Driver.class }): 类加载路径中必须有HikariDataSource类和com.mysql.jdbc.Driver才自动配置数据源
@Bean(destroyMethod = “close”): spring容器结束时调用数据源的close方法关闭数据源
@ConditionalOnMissingBean(DataSource.class): spring上下文中没有DataSource实例时才自动配置数据源
三、根据期望的使用方式进行不同配置
-
如果只是通过在启动类中增加 @Import(MysqlAutoConfiguration.class) 使用该starter,那么只需要做前面的工作就可以了。
-
通过在启动类中增加 @EnableMysql 的方式使用。
需要定义一个名为EnableMysql的注解,EnableMysql.java
package com.bruce.mysqlstart;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(MysqlAutoConfiguration.class)
public @interface EnableMysql {
}
- 希望在启动类中不增加任何额外配置,只将jar包放在classpath中。
新增META-INF/spring.factories文件内容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.bruce.mysqlstart.MysqlAutoConfiguration
参考
SpringBoot 自定义starter
自定义starter
Creating Your Own Starter
最后
以上就是拼搏飞机为你收集整理的Spring Boot(一)开发自己的starter的全部内容,希望文章能够帮你解决Spring Boot(一)开发自己的starter所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复