diff --git a/crane4j-core/src/main/java/cn/crane4j/core/support/query/AbstractQueryAssembleAnnotationHandler.java b/crane4j-core/src/main/java/cn/crane4j/core/support/query/AbstractQueryAssembleAnnotationHandler.java index 0c2b4139..c8e63437 100644 --- a/crane4j-core/src/main/java/cn/crane4j/core/support/query/AbstractQueryAssembleAnnotationHandler.java +++ b/crane4j-core/src/main/java/cn/crane4j/core/support/query/AbstractQueryAssembleAnnotationHandler.java @@ -25,6 +25,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -136,15 +137,32 @@ protected abstract MethodInvoker createMethodInvoker( * @param id id * @param repository repository * @return old repository target - * @see #ormRepositoryMap + * @see #getRepository */ - public abstract R registerRepository(String id, @NonNull R repository); + public synchronized R registerRepository(String id, @NonNull R repository) { + Asserts.isNotNull(repository, "repository cannot be null"); + QueryRepository repo = createRepository(id, repository); + return Optional.ofNullable(ormRepositoryMap.put(id, repo)) + .map(QueryRepository::getTarget) + .orElse(null); + } + + /** + * Create repository. + * + * @param id id + * @param repository repository + * @return repo + */ + @NonNull + protected abstract QueryRepository createRepository(String id, @NonNull R repository); /** * Get repository by given annotation. * * @param standardAnnotation standard annotation * @return repository + * @see #registerRepository */ protected synchronized QueryRepository getRepository(OrmAssembleAnnotation standardAnnotation) { QueryDefinition queryDefinition = standardAnnotation.getQueryDefinition(); @@ -204,22 +222,4 @@ protected static class OrmAssembleAnnotation extends StandardAssembleAnnotationAdapter implements StandardAssembleAnnotation { private final QueryDefinition queryDefinition; } - - /** - * Repository provider. - * - * @param repository type - * @author huangchengxing - */ - @FunctionalInterface - public interface RepositoryTargetProvider { - - /** - * Get repository by given id. - * - * @param queryDefinition query definition - * @return repository - */ - R get(QueryDefinition queryDefinition); - } } diff --git a/crane4j-core/src/main/java/cn/crane4j/core/support/query/RepositoryTargetProvider.java b/crane4j-core/src/main/java/cn/crane4j/core/support/query/RepositoryTargetProvider.java new file mode 100644 index 00000000..98a6d0b5 --- /dev/null +++ b/crane4j-core/src/main/java/cn/crane4j/core/support/query/RepositoryTargetProvider.java @@ -0,0 +1,19 @@ +package cn.crane4j.core.support.query; + +/** + * Repository provider. + * + * @param repository type + * @author huangchengxing + */ +@FunctionalInterface +public interface RepositoryTargetProvider { + + /** + * Get repository by given id. + * + * @param queryDefinition query definition + * @return repository + */ + R get(QueryDefinition queryDefinition); +} diff --git a/crane4j-extension/crane4j-extension-datasource/src/main/java/cn/crane4j/extension/query/AssembleDbAnnotationHandler.java b/crane4j-extension/crane4j-extension-datasource/src/main/java/cn/crane4j/extension/query/AssembleDbAnnotationHandler.java index f6c51f99..9ef316bd 100644 --- a/crane4j-extension/crane4j-extension-datasource/src/main/java/cn/crane4j/extension/query/AssembleDbAnnotationHandler.java +++ b/crane4j-extension/crane4j-extension-datasource/src/main/java/cn/crane4j/extension/query/AssembleDbAnnotationHandler.java @@ -10,6 +10,7 @@ import cn.crane4j.core.support.query.AbstractQueryAssembleAnnotationHandler; import cn.crane4j.core.support.query.QueryDefinition; import cn.crane4j.core.support.query.QueryRepository; +import cn.crane4j.core.support.query.RepositoryTargetProvider; import cn.crane4j.core.util.Asserts; import cn.crane4j.core.util.CollectionUtils; import cn.hutool.core.text.CharSequenceUtil; @@ -33,7 +34,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -181,18 +181,24 @@ public void setRepositoryTargetProvider( throw new UnsupportedOperationException("RepositoryTargetProvider is not supported to set!"); } + /** + * Create repository. + * + * @param id id + * @param queryDefinition repository + * @return repo + */ + @NonNull @Override - public QueryDefinition registerRepository(String id, @NonNull QueryDefinition queryDefinition) { + protected DataSourceRepository createRepository(String id, @NonNull QueryDefinition queryDefinition) { Asserts.isNotEmpty(queryDefinition.getConditionColumn(), "condition column cannot be empty!"); - return Optional.ofNullable(ormRepositoryMap.put(id, new DataSourceRepository(queryDefinition))) - .map(QueryRepository::getTarget) - .orElse(null); + return new DataSourceRepository(queryDefinition); } @SuppressWarnings("java:S6548") @Getter @RequiredArgsConstructor - private static class DataSourceRepository implements QueryRepository { + protected static class DataSourceRepository implements QueryRepository { private final QueryDefinition target; @Override public String getPrimaryKeyProperty() { diff --git a/crane4j-extension/crane4j-extension-mybatis-plus/src/main/java/cn/crane4j/extension/mybatis/plus/AssembleMpAnnotationHandler.java b/crane4j-extension/crane4j-extension-mybatis-plus/src/main/java/cn/crane4j/extension/mybatis/plus/AssembleMpAnnotationHandler.java index 167fa3d8..68b5103f 100644 --- a/crane4j-extension/crane4j-extension-mybatis-plus/src/main/java/cn/crane4j/extension/mybatis/plus/AssembleMpAnnotationHandler.java +++ b/crane4j-extension/crane4j-extension-mybatis-plus/src/main/java/cn/crane4j/extension/mybatis/plus/AssembleMpAnnotationHandler.java @@ -11,7 +11,6 @@ import cn.crane4j.core.support.query.AbstractQueryAssembleAnnotationHandler; import cn.crane4j.core.support.query.QueryDefinition; import cn.crane4j.core.support.query.QueryRepository; -import cn.crane4j.core.util.Asserts; import cn.crane4j.core.util.CollectionUtils; import cn.crane4j.core.util.StringUtils; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; @@ -28,7 +27,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.Objects; -import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -99,19 +97,16 @@ private String resolveSelectColumn(String column, BaseMapperQueryRepository repo } /** - * Register repository. + * Create repository. * * @param id id * @param repository repository - * @return old repository target + * @return repo */ + @NonNull @Override - public BaseMapper registerRepository(String id, @NonNull BaseMapper repository) { - Asserts.isNotNull(repository, "repository cannot be null"); - BaseMapperQueryRepository ormRepository = new BaseMapperQueryRepository(id, repository); - return Optional.ofNullable(ormRepositoryMap.put(id, ormRepository)) - .map(QueryRepository::getTarget) - .orElse(null); + protected BaseMapperQueryRepository createRepository(String id, @NonNull BaseMapper repository) { + return new BaseMapperQueryRepository(id, repository); } /** diff --git a/crane4j-spring-boot-starter/src/main/java/cn/crane4j/spring/boot/config/Crane4jMybatisPlusAutoConfiguration.java b/crane4j-spring-boot-starter/src/main/java/cn/crane4j/spring/boot/config/Crane4jMybatisPlusAutoConfiguration.java index 7713fe7b..3981d2c8 100644 --- a/crane4j-spring-boot-starter/src/main/java/cn/crane4j/spring/boot/config/Crane4jMybatisPlusAutoConfiguration.java +++ b/crane4j-spring-boot-starter/src/main/java/cn/crane4j/spring/boot/config/Crane4jMybatisPlusAutoConfiguration.java @@ -4,8 +4,9 @@ import cn.crane4j.core.support.AnnotationFinder; import cn.crane4j.core.support.Crane4jGlobalConfiguration; import cn.crane4j.core.support.container.MethodInvokerContainerCreator; -import cn.crane4j.core.support.query.AbstractQueryAssembleAnnotationHandler; import cn.crane4j.core.support.query.QueryDefinition; +import cn.crane4j.core.support.query.RepositoryTargetProvider; +import cn.crane4j.core.util.Try; import cn.crane4j.extension.mybatis.plus.AssembleMpAnnotationHandler; import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; import com.baomidou.mybatisplus.core.mapper.BaseMapper; @@ -104,7 +105,7 @@ public static class Properties { * @since 2.9.0 */ @RequiredArgsConstructor - public static class MapperLazyLoader implements AbstractQueryAssembleAnnotationHandler.RepositoryTargetProvider> { + public static class MapperLazyLoader implements RepositoryTargetProvider> { private final BeanFactory beanFactory; @@ -155,7 +156,10 @@ public void run(ApplicationArguments args) { AssembleMpAnnotationHandler handler = applicationContext.getBean(AssembleMpAnnotationHandler.class); mappers.entrySet().stream() .filter(e -> mapperFilter.test(e.getKey(), e.getValue())) - .forEach(e -> handler.registerRepository(e.getKey(), e.getValue())); + .forEach(e -> Try.of(() -> handler.registerRepository(e.getKey(), e.getValue())) + .subscribeFailure(ex -> log.error("Failed to register mapper: {}", e.getKey(), ex)) + .perform() + ); log.info("crane4j mybatis-plus extension component initialization completed."); } }