亲宝软件园·资讯

展开

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

不会压弯的小飞侠 人气:0

开发步骤

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

重新安装,再次测试

加载全部内容

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