创建项目 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。
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 文件中的配置信息。
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; } }
spring.jdbc.datasource.type=c3p0
,使用 c3p0 数据源。spring.jdbc.datasource.type=druid
,使用 druid 数据源。此处使用条件选择实现,@ConditionalOnProperty(value = "spring.jdbc.datasource.type",havingValue = "druid")
根据配置文件中的 type
值进行条件选择创建不同的 DataSource
。
在resource
文件夹下面新建 META-INF/spring.factories
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.shwen.autoconfig.DataSourceAutoConfiguration
注意要执行 install ,安装项目。
pom
文件:
<dependency> <groupId>com.shwen</groupId> <artifactId>spring-boot-jdbc-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
application.yml
:
配置文件配置 type
指定需要创建的数据源,此处指定数据源为druid
,并且配置连接属性信息。
@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项目启动后,
反射
实例化标注了@Configuration
的 IOC容器配置类,然后汇总并加载到 IOC 容器。当收集到所有在 spring.factories 中指定的bean后,再通过@Import
注解将其导入进容器。@ConfigurationProperties
注解会读取配置文件 application.yml 里面属性,将读到的值绑定到被注解类的属性。然后,@EnableConfigurationProperties
将属性配置类注入到 IOC 容器中,这样当项目需要配置信息的时候可以直接从容器中去取。@Autowired
装配使用。到此这篇关于springboot 自定义启动器的实现的文章就介绍到这了,更多相关springboot 自定义启动器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
编程 | 2023-02-24 21:36
编程 | 2023-02-21 12:51
编程 | 2023-02-21 12:47
编程 | 2023-02-21 00:15
编程 | 2023-02-21 00:08
编程 | 2023-02-20 21:46
编程 | 2023-02-20 21:42
编程 | 2023-02-20 21:36
编程 | 2023-02-20 21:32
编程 | 2023-02-20 18:12
网友评论