Skip to content

Commit

Permalink
Multi module support set up
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhi13man committed Jul 13, 2024
1 parent e8cbaba commit e1e0dae
Show file tree
Hide file tree
Showing 14 changed files with 790 additions and 510 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.annotation.Nonnull;

/**
* Annotation to enable multi data source configuration for the service.
Expand All @@ -12,19 +13,6 @@
@Retention(RetentionPolicy.SOURCE)
public @interface EnableMultiDataSourceConfig {

/**
* The array of exact packages to scan for entities.
* <p>
* This must be an exact package name, and not a prefix as custom entity managers can not
* recursively scan entities inside nested packages.
* <p>
* If {@link TargetSecondaryDataSource} is used on any repository, the package of the entity that
* the repository is associated with will also be scanned for entities.
*
* @return the array of exact packages to scan for entities.
*/
String[] exactEntityPackages() default {};

/**
* The array of packages to scan for repositories.
* <p>
Expand All @@ -33,14 +21,14 @@
*
* @return the array of packages to scan for repositories.
*/
String[] repositoryPackages() default {};
@Nonnull String[] repositoryPackages() default {};

/**
* The prefix of the properties for each of the data sources in the application properties file.
*
* @return the prefix of the data source properties in the application properties file.
*/
String datasourcePropertiesPrefix() default "spring.datasource";
@Nonnull String datasourcePropertiesPrefix() default "spring.datasource";

/**
* The package where the generated data source configs will be placed.
Expand All @@ -55,23 +43,7 @@
*
* @return the package where the generated data source configs will be placed.
*/
String generatedConfigPackage() default "";

/**
* The prefix of the package where the generated copies of the repositories will be placed.
* <p>
* If this is not provided, the generated repositories will be placed in the same package as the
* class annotated with @EnableMultiDataSourceConfig followed by .generated.repositories and then
* .{snake_case_data_source_name}
* <p>
* The generated repositories will be placed in packages with the following format:
* <p>
* {generatedRepositoryPackagePrefix}.{PascalCaseDataSourceName}{AnnotatedMethodRepositoryName}
*
* @return the prefix of the package where the generated copies of the repositories will be
* placed.
*/
String generatedRepositoryPackagePrefix() default "";
@Nonnull String generatedConfigPackage() default "";

/**
* The config for the primary data source.
Expand All @@ -81,7 +53,7 @@
*
* @return the {@link DataSourceConfig} annotation for the primary data source.
*/
DataSourceConfig primaryDataSourceConfig() default @DataSourceConfig(dataSourceName = "master");
@Nonnull DataSourceConfig primaryDataSourceConfig() default @DataSourceConfig(dataSourceName = "master");

/**
* The array of {@link DataSourceConfig} annotations which contain the configuration for each
Expand All @@ -90,7 +62,7 @@
* @return the array of {@link DataSourceConfig} annotations.
* @see DataSourceConfig
*/
DataSourceConfig[] secondaryDataSourceConfigs() default {};
@Nonnull DataSourceConfig[] secondaryDataSourceConfigs() default {};

@Retention(RetentionPolicy.RUNTIME)
@Target({})
Expand All @@ -110,14 +82,14 @@
*
* @return the name of the data source.
*/
String dataSourceName() default "";
@Nonnull String dataSourceName() default "";

/**
* The application properties key/path of the data source class properties.
*
* @return the prefix of the data source class properties in the application properties file.
*/
String dataSourceClassPropertiesPath() default "spring.datasource.hikari";
@Nonnull String dataSourceClassPropertiesPath() default "spring.datasource.hikari";

/**
* The application properties key/path under which the JPA properties to override for this data
Expand All @@ -128,6 +100,19 @@
*
* @return the key under which the JPA properties to override are located.
*/
String overridingJpaPropertiesPath() default "spring.jpa.properties";
@Nonnull String overridingJpaPropertiesPath() default "spring.jpa.properties";

/**
* The array of exact packages to scan for entities.
* <p>
* This must be an exact package name, and not a prefix as custom entity managers can not
* recursively scan entities inside nested packages.
* <p>
* If {@link TargetSecondaryDataSource} is used on any repository, the package of the entity
* that the repository is associated with will also be scanned for entities.
*
* @return the array of exact packages to scan for entities.
*/
@Nonnull String[] exactEntityPackages() default {};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Will generate all relevant boilerplate code and beans.
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.CLASS)
@Repeatable(TargetSecondaryDataSources.class)
public @interface TargetSecondaryDataSource {

Expand All @@ -23,6 +23,14 @@
* <p>
* To use a data source other than the primary, it must have been configured in the
* {@link EnableMultiDataSourceConfig#secondaryDataSourceConfigs()} annotations.
* <p>
* The generated repositories will be placed in the same package as the class annotated with
* {@link EnableMultiDataSourceConfig} followed by .generated.repositories and then
* .{snake_case_data_source_name}
* <p>
* The generated repositories will be placed in packages with the following format:
* <p>
* {generatedRepositoryPackagePrefix}.{PascalCaseDataSourceName}{AnnotatedMethodRepositoryName}
*
* @return the data source to use for the repository.
* @see EnableMultiDataSourceConfig.DataSourceConfig#dataSourceName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Will generate all relevant boilerplate code and beans.
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.CLASS)
public @interface TargetSecondaryDataSources {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.dhi13man.spring.datasource.config;

/**
* This interface is implemented by all the generated Multi Data Source Repository classes.
*/
public interface IGeneratedDataSourceRepository {

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package io.github.dhi13man.spring.datasource.config;

import java.util.Properties;
Expand All @@ -14,7 +9,7 @@
import org.springframework.transaction.PlatformTransactionManager;

/**
* This interface is followed by all the generated Multi Data Source Config classes.
* This interface is implemented by all the generated Multi Data Source Config classes.
*/
public interface IMultiDataSourceConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ public final class MultiDataSourceErrorConstants {
+ " @DataSourceConfigs annotations are provided for the same data source. Please"
+ " provide only one @DataSourceConfigs annotation for each data source.";

public static final String NO_ENTITY_PACKAGES_PROVIDED_IN_CONFIG = "No entity packages are"
+ " provided in @EnableMultiDataSourceConfig.exactEntityPackages. Please provide all the"
+ " exact packages that hold your entities.";

public static final String NO_REPOSITORY_PACKAGES_PROVIDED_IN_CONFIG = "No repository packages"
+ " are provided in @EnableMultiDataSourceConfig.repositoryPackages. Please provide all"
+ " the packages (or a parent package) that hold your repositories.";
Expand All @@ -23,11 +19,6 @@ public final class MultiDataSourceErrorConstants {
+ " one repository method with this annotation if you are using"
+ " @EnableMultiDataSourceConfig and want to segregate your repositories by data source.";

public static final String REPOSITORY_METHODS_SHOULD_NOT_HAVE_PRIMARY_DATA_SOURCE_AS_TARGET =
"Repository methods should not be annotated with @TargetSecondaryDataSource as the primary "
+ "data source, as all repositories will use the primary data source by default. Please "
+ "remove the @TargetSecondaryDataSource annotation from the following methods: ";

private MultiDataSourceErrorConstants() {
}

Expand Down
Loading

0 comments on commit e1e0dae

Please sign in to comment.