java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot自定义启动器

SpringBoot实现自定义启动器的示例详解

作者:不会压弯的小飞侠

虽然Spring官方给我们提供了很多的启动器供我们使用,但有时候我们也会遇到某些特殊场景,这些启动器满足不了。这个时候就需要自定义一个启动器供我们使用,本文为大家介绍了SpringBoot实现自定义启动器的方法,希望对大家有所帮助

开发步骤

1.创建启动器项目

测试

创建个SpringBoot项目,导入依赖

<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.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>

创建属性配置类

@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 + '\'' +
                '}';
    }
}

创建自动配置类

@SpringBootConfiguration  //当前类是个配置类
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceAutoConfiguration {
    @Autowired
    private DataSourceProperties dataSourceProperties;

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

}

编写自动配置属性文件

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

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.study.config.DataSourceAutoConfiguration

执行install , 安装项目

自定义启动器

重新创建个项目,引用SpringBoot_datasource的依赖

<!--依赖自定义启动器-->
		<dependency>
			<groupId>com.study</groupId>
			<artifactId>SpringBoot_datasource</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>

配置连接池信息

server.port=8081
spring.jdbc.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jdbc.datasource.url=jdbc:mysql:///springboot_datasource
spring.jdbc.datasource.username=root
spring.jdbc.datasource.password=root
spring.profiles.active=datasource

编写Controller类

@RestController
public class HelloAutoController {
    @Autowired
    private DataSource dataSource;

    @RequestMapping("/dataSource")
    public String dataSource() {
        System.out.println(dataSource.getClass());//打印DruidDataSource数据源
        return "Hello dataSource! ! ! " ;
    }

}

使用自定义启动类进行测试

启动器支持多Druid和C3P0

添加创建druid和c3p0方法

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

}

配置文件中配置了spring.jdbc.datasource.type=druid使用druid数据源

server.port=8081
spring.jdbc.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jdbc.datasource.url=jdbc:mysql:///springboot_datasource
spring.jdbc.datasource.username=root
spring.jdbc.datasource.password=root
spring.jdbc.datasource.type=druid

重新安装,再次测试

配置了spring.jdbc.datasource.type=c3p0使用c3p0数据源

server.port=8081
spring.jdbc.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jdbc.datasource.url=jdbc:mysql:///springboot_datasource
spring.jdbc.datasource.username=root
spring.jdbc.datasource.password=root
spring.jdbc.datasource.type=c3p0

重新安装,再次测试

到此这篇关于SpringBoot实现自定义启动器的示例详解的文章就介绍到这了,更多相关SpringBoot自定义启动器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文