当前位置:首页 » 《随便一记》 » 正文

自定义启动器

16 人参与  2023年01月15日 19:13  分类 : 《随便一记》  评论

点击全文阅读


?博客主页:?不会压弯的小飞侠
✨欢迎关注:?点赞?收藏⭐留言✒
✨系列专栏:?SpringBoot专栏
?欢迎大佬指正,一起学习!一起加油!

在这里插入图片描述

目录

?开发步骤?测试?自定义启动器?启动器支持多Druid和C3P0


?开发步骤

1.创建启动器项目

添加启动器相关依赖创建属性配置类创建自动配置类编写自动配置文件(spring.factories)使用自定义的启动器

?测试

创建个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=8081spring.jdbc.datasource.driverClassName=com.mysql.jdbc.Driverspring.jdbc.datasource.url=jdbc:mysql:///springboot_datasourcespring.jdbc.datasource.username=rootspring.jdbc.datasource.password=rootspring.profiles.active=datasource
编写Controller类
@RestControllerpublic 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=8081spring.jdbc.datasource.driverClassName=com.mysql.jdbc.Driverspring.jdbc.datasource.url=jdbc:mysql:///springboot_datasourcespring.jdbc.datasource.username=rootspring.jdbc.datasource.password=rootspring.jdbc.datasource.type=druid
重新安装,再次测试

在这里插入图片描述

配置了spring.jdbc.datasource.type=c3p0使用c3p0数据源
server.port=8081spring.jdbc.datasource.driverClassName=com.mysql.jdbc.Driverspring.jdbc.datasource.url=jdbc:mysql:///springboot_datasourcespring.jdbc.datasource.username=rootspring.jdbc.datasource.password=rootspring.jdbc.datasource.type=c3p0
重新安装,再次测试

在这里插入图片描述


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/51530.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1