Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yuanwenqing/springboot-support
some extension and support for spring boot
https://github.com/yuanwenqing/springboot-support
datasource multi-datasource spring spring-boot spring-boot-extend
Last synced: 17 days ago
JSON representation
some extension and support for spring boot
- Host: GitHub
- URL: https://github.com/yuanwenqing/springboot-support
- Owner: YuanWenqing
- License: mit
- Created: 2018-08-28T10:34:04.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-09-06T02:44:39.000Z (about 6 years ago)
- Last Synced: 2024-10-10T05:37:09.619Z (about 1 month ago)
- Topics: datasource, multi-datasource, spring, spring-boot, spring-boot-extend
- Language: Java
- Size: 71.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# springboot-support
some extended supports for SpringBoot## multi-datasource
Support multi-datasource for SpringBoot.
### How To Enable Multi-DataSource Support?
Just do it in a SpringBoot way:
~~~java
@Configuration
@EnableMultiDataSource
public class XXXConfiguration {
// code...
}
~~~or
~~~java
@SpringBootApplication
@EnableMultiDataSource
public class XXXApplication {
// code...
}
~~~In purpose to specify your configuration of multi-datasource, inside `EnableMultiDataSource` you can change:
* `location`: location of configuration properties, default `classpath:application.properties`.
* `prefix`: prefix of configuration, default `multi-datasource.multi`.
* `loader`: class of loader to load configuration properties, default `DefaultPropertiesLoader`; If some ConfigurationService, like Spring Cloud Config or Aliyun ACM, is in use, change `loader` to a customized loader class implementing `PropertiesLoader` interface to load it in your way.### How To Configure Multi-DataSource?
Configuration properties is parsed by `PropertiesParser`. As a convention, every item will be treated in a `...` pattern:
* `prefix`: `prefix` defined in `EnableMultiDataSource`
* `name`: base name of DataSource and JdbcTemplate beans, thus generating bean names in pattern `DataSource` and `JdbcTemplate`
* `config`: a sub-name for detail config properties of DataSource, candidates is below
* `datasource`: basic config properties, bind to `DataSourceProperties`
* `hikari`: Hikari config, bind to `com.zaxxer.hikari.HikariDataSource`
* `tomcat`: Tomcat config, bind to `org.apache.tomcat.jdbc.pool.DataSource`
* `dbcp2`: Dbcp2 config, bind to `org.apache.commons.dbcp2.BasicDataSource`
* `property`: property name in `DataSourceProperties`; We reuse utilized classes `DataSourceProperties` and `DataSourceBuilder` in SpringBoot to construct DataSource and JdbcTemplateType of DataSource will be determined by SpringBoot automatically if not specified in properties,
just like `spring-boot-starter-jdbc` does.Example:
~~~properties
multi-datasource.multi.a.datasource.url = jdbc:mysql://localhost:3306/test
multi-datasource.multi.a.datasource.username=test
multi-datasource.multi.a.datasource.password=123456
multi-datasource.multi.a.datasource.driver-class-name=com.mysql.cj.jdbc.Drivermulti-datasource.multi.b.datasource.url = jdbc:mysql://localhost:3306/test
multi-datasource.multi.b.datasource.username=test
multi-datasource.multi.b.datasource.password=123456
multi-datasource.multi.b.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
multi-datasource.multi.b.hikari.maximum-pool-size = 3
~~~This will lead to construction of:
* 2 DataSources with bean names: `aDataSource` and `bDataSource`
* 2 JdbcTemplates with bean names: `aJdbcTemplate` and `bJdbcTemplate`### How To Inject DataSource/JdbcTemplate?
Beans depending on a DataSource or JdbcTemplate must be annotated by `DataSourceRouting`,
and `value` should be one of base names parsed from configuration properties.
Besides, to accept DataSource or JdbcTemplate specified by `DataSourceRouting`,
bean also must satisfied one of the following convention:* implement interface `DataSourceAware` or `JdbcTemplateAware`.
* own a method named as `setDataSource` or `setJdbcTemplate`.Example:
~~~java
@DataSourceRouting("a")
public class JdbcTemplateAwareBean implements JdbcTemplateAware {
private JdbcTemplate jdbcTemplate;
@Override
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
~~~or
~~~java
@DataSourceRouting("a")
public class JdbcTemplateSetterBean {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
~~~Injection of DataSource and JdbcTemplate is processed by `DataSourceRoutingPostProcessor`,
which will detect interfaces and methods introduced above and do the injection.