-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
273 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
spring-turbo-module-jackson/src/main/java/spring.turbo.module.jackson/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@NonNullApi | ||
@NonNullFields | ||
package spring.turbo.module.jackson; | ||
|
||
import org.springframework.lang.NonNullApi; | ||
import org.springframework.lang.NonNullFields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<parent> | ||
<groupId>com.github.yingzhuo</groupId> | ||
<artifactId>spring-turbo-modules</artifactId> | ||
<version>3.4.1-3</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>spring-turbo-module-jdbc</artifactId> | ||
|
||
<dependencies> | ||
<!-- spring-turbo & modules --> | ||
<dependency> | ||
<groupId>com.github.yingzhuo</groupId> | ||
<artifactId>spring-turbo</artifactId> | ||
</dependency> | ||
|
||
<!-- spring-boot --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-aop</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-jdbc</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- lombok --> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
4 changes: 4 additions & 0 deletions
4
...n/java/spring/turbo/module/jdbc/autoconfiguration/RoutingDataSourceAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package spring.turbo.module.jdbc.autoconfiguration; | ||
|
||
public class RoutingDataSourceAutoConfiguration { | ||
} |
6 changes: 6 additions & 0 deletions
6
...bo-module-jdbc/src/main/java/spring/turbo/module/jdbc/autoconfiguration/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@NonNullApi | ||
@NonNullFields | ||
package spring.turbo.module.jdbc.autoconfiguration; | ||
|
||
import org.springframework.lang.NonNullApi; | ||
import org.springframework.lang.NonNullFields; |
23 changes: 23 additions & 0 deletions
23
spring-turbo-module-jdbc/src/main/java/spring/turbo/module/jdbc/ds/DataSourceSwitch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package spring.turbo.module.jdbc.ds; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* 数据源切换 | ||
* | ||
* @author 应卓 | ||
* @since 3.4.1 | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface DataSourceSwitch { | ||
|
||
/** | ||
* 数据源名称 | ||
* | ||
* @return 数据源名称 | ||
*/ | ||
public String value(); | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...g-turbo-module-jdbc/src/main/java/spring/turbo/module/jdbc/ds/DataSourceSwitchAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package spring.turbo.module.jdbc.ds; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.core.Ordered; | ||
|
||
@Aspect | ||
public record DataSourceSwitchAdvice(int order) implements Ordered { | ||
|
||
@Around("@annotation(spring.turbo.module.jdbc.ds.DataSourceSwitch)") | ||
public Object around(ProceedingJoinPoint joinPoint) throws Throwable { | ||
var signature = joinPoint.getSignature(); | ||
if (signature instanceof MethodSignature methodSignature) { | ||
var annotation = methodSignature.getMethod().getAnnotation(DataSourceSwitch.class); | ||
var dataSourceName = annotation.value(); | ||
RoutingDataSourceLookup.set(dataSourceName); | ||
try { | ||
return joinPoint.proceed(); | ||
} finally { | ||
RoutingDataSourceLookup.remove(); | ||
} | ||
} else { | ||
return joinPoint.proceed(); | ||
} | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return order; | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
spring-turbo-module-jdbc/src/main/java/spring/turbo/module/jdbc/ds/RoutingDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package spring.turbo.module.jdbc.ds; | ||
|
||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; | ||
import org.springframework.jdbc.datasource.lookup.MapDataSourceLookup; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* 路由数据源 | ||
* | ||
* @author 应卓 | ||
* @see AbstractRoutingDataSource | ||
* @see RoutingDataSourceLookup | ||
* @since 3.4.1 | ||
*/ | ||
public class RoutingDataSource extends AbstractRoutingDataSource implements DataSource, InitializingBean { | ||
|
||
public RoutingDataSource(String defaultDataSourceName, Map<String, DataSource> targetDataSources) { | ||
Assert.hasText(defaultDataSourceName, "defaultDataSourceName is required"); | ||
Assert.notEmpty(targetDataSources, "targetDataSources is null or empty"); | ||
|
||
super.setDefaultTargetDataSource(defaultDataSourceName); | ||
super.setTargetDataSources(new HashMap<>(targetDataSources)); | ||
super.setDataSourceLookup(new MapDataSourceLookup()); | ||
} | ||
|
||
public RoutingDataSource(DataSource defaultDataSource, Map<String, DataSource> targetDataSources) { | ||
Assert.notNull(defaultDataSource, "defaultDataSource is required"); | ||
Assert.notEmpty(targetDataSources, "targetDataSources is null or empty"); | ||
|
||
super.setDefaultTargetDataSource(defaultDataSource); | ||
super.setTargetDataSources(new HashMap<>(targetDataSources)); | ||
super.setDataSourceLookup(new MapDataSourceLookup()); | ||
} | ||
|
||
@Override | ||
@Nullable | ||
protected Object determineCurrentLookupKey() { | ||
return RoutingDataSourceLookup.get(); | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
super.afterPropertiesSet(); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...-turbo-module-jdbc/src/main/java/spring/turbo/module/jdbc/ds/RoutingDataSourceLookup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package spring.turbo.module.jdbc.ds; | ||
|
||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* 路由数据源查找策略 | ||
* | ||
* @author 应卓 | ||
* @since 3.4.1 | ||
*/ | ||
public final class RoutingDataSourceLookup { | ||
|
||
private static final ThreadLocal<String> DS_NAME_HOLDER = ThreadLocal.withInitial(() -> null); | ||
|
||
/** | ||
* 私有构造方法 | ||
*/ | ||
private RoutingDataSourceLookup() { | ||
super(); | ||
} | ||
|
||
public static void set(String dataSourceName) { | ||
Assert.hasText(dataSourceName, "dataSourceName is required"); | ||
DS_NAME_HOLDER.set(dataSourceName); | ||
} | ||
|
||
@Nullable | ||
public static String get() { | ||
return DS_NAME_HOLDER.get(); | ||
} | ||
|
||
public static void remove() { | ||
DS_NAME_HOLDER.remove(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...-turbo-module-jdbc/src/main/java/spring/turbo/module/jdbc/ds/hikari/HikariProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package spring.turbo.module.jdbc.ds.hikari; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; | ||
|
||
import java.io.Serializable; | ||
|
||
@Getter | ||
@Setter | ||
public class HikariProperties implements JdbcConnectionDetails, Serializable { | ||
|
||
private String jdbcUrl; | ||
private String username; | ||
private String password; | ||
private String driverClassName; | ||
|
||
private String poolName = null; | ||
private int minimumIdle = 10; | ||
private int maximumPoolSize = 30; | ||
private boolean autoCommit = true; | ||
private long idleTimeout = 30000L; | ||
private long maxLifetime = 900000L; | ||
private long connectionTimeout = 10000; | ||
private String connectionTestQuery = "SELECT 1 FROM DUAL"; | ||
private long validationTimeout = 1000L; | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
spring-turbo-module-jdbc/src/main/java/spring/turbo/module/jdbc/ds/hikari/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@NonNullApi | ||
@NonNullFields | ||
package spring.turbo.module.jdbc.ds.hikari; | ||
|
||
import org.springframework.lang.NonNullApi; | ||
import org.springframework.lang.NonNullFields; |
6 changes: 6 additions & 0 deletions
6
spring-turbo-module-jdbc/src/main/java/spring/turbo/module/jdbc/ds/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@NonNullApi | ||
@NonNullFields | ||
package spring.turbo.module.jdbc.ds; | ||
|
||
import org.springframework.lang.NonNullApi; | ||
import org.springframework.lang.NonNullFields; |
6 changes: 6 additions & 0 deletions
6
spring-turbo-module-jdbc/src/main/java/spring/turbo/module/jdbc/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@NonNullApi | ||
@NonNullFields | ||
package spring.turbo.module.jdbc; | ||
|
||
import org.springframework.lang.NonNullApi; | ||
import org.springframework.lang.NonNullFields; |
File renamed without changes.
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
spring.turbo.module.jdbc.autoconfiguration.RoutingDataSourceAutoConfiguration |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters