亲宝软件园·资讯

展开

springboot 自定义启动器的实现

不会叫的狼 人气:0

创建自定义启动器

0、项目总览

1、创建项目,引入依赖

创建项目 spring-boot-jdbc-starter,引入依赖,pom文件如下:

<?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.shwen</groupId>
  <artifactId>spring-boot-jdbc-starter</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>spring-boot-jdbc-starter</name>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </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.12</version>
    </dependency>

    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>

</project>

启动器支持多种数据源,例如:C3P0和Druid,根据配置的type进行选择。此处我们引用两个连接池依赖,一个是C3P0,一个是Druid。

2、创建属性配置类

package com.shwen.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "spring.jdbc.datasource")
public class DataSourceProperties {
    private String driverClassName;
    private String url;
    private String username;
    private String password;

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "DataSourceProperties{" +
                "driverClassName='" + driverClassName + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

属性配置类注入 application.yml 文件中的配置信息。

3、创建自动配置类

package com.shwen.autoconfig;

import ...
import javax.sql.DataSource;

@SpringBootConfiguration
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceAutoConfiguration {
    @Autowired
    private DataSourceProperties dataSourceProperties ;

    @Bean
    @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
    @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;
    }
}

此处使用条件选择实现,@ConditionalOnProperty(value = "spring.jdbc.datasource.type",havingValue = "druid") 根据配置文件中的 type 值进行条件选择创建不同的 DataSource

4、创建自动配置属性文件

resource文件夹下面新建 META-INF/spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.shwen.autoconfig.DataSourceAutoConfiguration

注意要执行 install ,安装项目。

使用自定义启动器

1、springboot_01 项目引入自定义启动器

pom文件:

<dependency>
  <groupId>com.shwen</groupId>
  <artifactId>spring-boot-jdbc-starter</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

2、配置连接池信息

application.yml

配置文件配置 type 指定需要创建的数据源,此处指定数据源为druid,并且配置连接属性信息。

3、注入属性测试

@RestController
public class HelloController {

    @Autowired
    private DataSource dataSource;

    @RequestMapping(path = "/hello2")
    public String sayHello2() throws SQLException {
        System.out.println(dataSource.getClass());
        
        DruidDataSource dds = (DruidDataSource) dataSource;
        System.out.println(dds.getPassword());
        return "hello spring boot";
    }
}

注入DataSource,查看连接池属性。

访问 http://127.0.0.1:8080/hello2,打印结果:

小结

通过编写自定义启动器,我们更加理解springboot自动配置原理。springboot项目启动后,

加载全部内容

相关教程
猜你喜欢
用户评论