From 00e3912a64ad9fd8d72ecfb31ae493e7edcc9813 Mon Sep 17 00:00:00 2001 From: noear Date: Sat, 18 Feb 2023 15:45:30 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=B0=83=E6=95=B4=20context.hasWrap=20?= =?UTF-8?q?=E6=9E=81=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bs/solon/BeanSearcherAutoConfiguration.java | 16 ++++++++-------- .../zhxu/bs/solon/JsonFieldConvertorConfig.java | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java b/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java index 5fc07e1c..befc3345 100644 --- a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java +++ b/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java @@ -340,7 +340,7 @@ public class BeanSearcherAutoConfiguration { @Condition(hasProperty = "${bean-searcher.field-convertor.use-list}=true") public ListFieldConvertor listFieldConvertor(BeanSearcherProperties config, ObjectProvider>> convertorsProvider) { - if(context.hasWrap(ListFieldConvertor.class) == false){ + if(context.hasWrap(ListFieldConvertor.class)){ return null; } @@ -352,7 +352,7 @@ public class BeanSearcherAutoConfiguration { @Bean public BeanReflector beanReflector(ObjectProvider> convertorsProvider) { - if(context.hasWrap(BeanReflector.class) == false){ + if(context.hasWrap(BeanReflector.class)){ return null; } @@ -365,7 +365,7 @@ public class BeanSearcherAutoConfiguration { @Bean public DbMapping dbMapping(BeanSearcherProperties config) { - if(context.hasWrap(DbMapping.class) == false){ + if(context.hasWrap(DbMapping.class)){ return null; } @@ -384,7 +384,7 @@ public class BeanSearcherAutoConfiguration { @Bean public MetaResolver metaResolver(DbMapping dbMapping, ObjectProvider snippetResolver) { - if(context.hasWrap(MetaResolver.class) == false){ + if(context.hasWrap(MetaResolver.class)){ return null; } @@ -402,7 +402,7 @@ public class BeanSearcherAutoConfiguration { BeanReflector beanReflector, ObjectProvider> interceptors, ObjectProvider> processors) { - if(context.hasWrap(BeanSearcher.class) == false){ + if(context.hasWrap(BeanSearcher.class)){ return null; } @@ -420,7 +420,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(hasProperty = "${bean-searcher.field-convertor.use-date-format}=true") public DateFormatFieldConvertor dateFormatFieldConvertor(BeanSearcherProperties config) { - if(context.hasWrap(DateFormatFieldConvertor.class) == false){ + if(context.hasWrap(DateFormatFieldConvertor.class)){ return null; } @@ -444,7 +444,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(hasProperty = "${bean-searcher.field-convertor.use-b2-m}=true") public B2MFieldConvertor b2mFieldConvertor(ObjectProvider> convertors) { - if(context.hasWrap(B2MFieldConvertor.class) == false){ + if(context.hasWrap(B2MFieldConvertor.class)){ return null; } @@ -465,7 +465,7 @@ public class BeanSearcherAutoConfiguration { ObjectProvider> interceptors, ObjectProvider> resultFilters) { - if(context.hasWrap(MapSearcher.class) == false){ + if(context.hasWrap(MapSearcher.class)){ return null; } diff --git a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/JsonFieldConvertorConfig.java b/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/JsonFieldConvertorConfig.java index 572e9091..81e57d88 100644 --- a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/JsonFieldConvertorConfig.java +++ b/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/JsonFieldConvertorConfig.java @@ -20,7 +20,7 @@ public class JsonFieldConvertorConfig { @Bean public JsonFieldConvertor jsonFieldConvertor(BeanSearcherProperties config) { - if(context.hasWrap(JsonFieldConvertor.class) == false){ + if(context.hasWrap(JsonFieldConvertor.class)){ return null; } -- Gitee From 9693d7155da42c161549106a4dd3d99ba6372480 Mon Sep 17 00:00:00 2001 From: noear Date: Sun, 19 Feb 2023 17:35:58 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=AE=8C=E5=96=84=20bean-search-solon-plug?= =?UTF-8?q?in=20=E9=80=82=E9=85=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bs/solon/JsonFieldConvertorConfig.java | 30 -- .../zhxu/bs/solon/beans/BeansException.java | 17 -- .../cn/zhxu/bs/solon/beans/ObjectFactory.java | 10 - .../zhxu/bs/solon/beans/ObjectProvider.java | 60 ---- .../pom.xml | 2 +- .../solon/BeanSearcherAutoConfiguration.java | 263 +++++++----------- .../zhxu/bs/solon/BeanSearcherProperties.java | 0 .../zhxu/bs/solon/IllegalConfigException.java | 0 .../cn/zhxu/bs/solon/NamedDataSource.java | 0 .../bs/solon/integration/XPluginImpl.java | 5 +- .../solon/bean-search-solon-plugin.properties | 0 pom.xml | 4 +- 12 files changed, 100 insertions(+), 291 deletions(-) delete mode 100644 bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/JsonFieldConvertorConfig.java delete mode 100644 bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/BeansException.java delete mode 100644 bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/ObjectFactory.java delete mode 100644 bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/ObjectProvider.java rename {bean-search-solon-plugin => bean-searcher-solon-plugin}/pom.xml (94%) rename {bean-search-solon-plugin => bean-searcher-solon-plugin}/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java (68%) rename {bean-search-solon-plugin => bean-searcher-solon-plugin}/src/main/java/cn/zhxu/bs/solon/BeanSearcherProperties.java (100%) rename {bean-search-solon-plugin => bean-searcher-solon-plugin}/src/main/java/cn/zhxu/bs/solon/IllegalConfigException.java (100%) rename {bean-search-solon-plugin => bean-searcher-solon-plugin}/src/main/java/cn/zhxu/bs/solon/NamedDataSource.java (100%) rename {bean-search-solon-plugin => bean-searcher-solon-plugin}/src/main/java/cn/zhxu/bs/solon/integration/XPluginImpl.java (80%) rename {bean-search-solon-plugin => bean-searcher-solon-plugin}/src/main/resources/META-INF/solon/bean-search-solon-plugin.properties (100%) diff --git a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/JsonFieldConvertorConfig.java b/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/JsonFieldConvertorConfig.java deleted file mode 100644 index 81e57d88..00000000 --- a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/JsonFieldConvertorConfig.java +++ /dev/null @@ -1,30 +0,0 @@ -package cn.zhxu.bs.solon; - -import cn.zhxu.bs.convertor.JsonFieldConvertor; -import cn.zhxu.xjson.JsonKit; -import org.noear.solon.annotation.Bean; -import org.noear.solon.annotation.Condition; -import org.noear.solon.annotation.Configuration; -import org.noear.solon.annotation.Inject; -import org.noear.solon.core.AopContext; - -/** - * 注解 @ConditionalOnClass 不能与 @Bean 放在一起,否则当没有条件中的 Class 时,会出现错误: - * java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy - */ -@Condition(hasClass = JsonKit.class, hasProperty = "${bean-searcher.field-convertor.use-json}=true") -@Configuration -public class JsonFieldConvertorConfig { - @Inject - AopContext context; - - @Bean - public JsonFieldConvertor jsonFieldConvertor(BeanSearcherProperties config) { - if(context.hasWrap(JsonFieldConvertor.class)){ - return null; - } - - BeanSearcherProperties.FieldConvertor conf = config.getFieldConvertor(); - return new JsonFieldConvertor(conf.isJsonFailOnError()); - } -} \ No newline at end of file diff --git a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/BeansException.java b/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/BeansException.java deleted file mode 100644 index b9f7de6e..00000000 --- a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/BeansException.java +++ /dev/null @@ -1,17 +0,0 @@ -package cn.zhxu.bs.solon.beans; - -import org.noear.solon.lang.Nullable; - -/** - * @author noear 2023/2/18 created - */ -public abstract class BeansException extends RuntimeException { - public BeansException(String msg) { - super(msg); - } - - public BeansException(@Nullable String msg, @Nullable Throwable cause) { - super(msg, cause); - } -} - diff --git a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/ObjectFactory.java b/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/ObjectFactory.java deleted file mode 100644 index 9f595ac2..00000000 --- a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/ObjectFactory.java +++ /dev/null @@ -1,10 +0,0 @@ -package cn.zhxu.bs.solon.beans; - -/** - * @author noear 2023/2/18 created - */ -@FunctionalInterface -public interface ObjectFactory { - T getObject() throws BeansException; -} - diff --git a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/ObjectProvider.java b/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/ObjectProvider.java deleted file mode 100644 index a72fd5a2..00000000 --- a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/beans/ObjectProvider.java +++ /dev/null @@ -1,60 +0,0 @@ -package cn.zhxu.bs.solon.beans; - -import org.noear.solon.lang.Nullable; - -import java.util.Iterator; -import java.util.function.Consumer; -import java.util.function.Supplier; -import java.util.stream.Stream; - -/** - * @author noear 2023/2/18 created - */ - -public interface ObjectProvider extends ObjectFactory, Iterable { - T getObject(Object... args) throws BeansException; - - @Nullable - T getIfAvailable() throws BeansException; - - default T getIfAvailable(Supplier defaultSupplier) throws BeansException { - T dependency = this.getIfAvailable(); - return dependency != null ? dependency : defaultSupplier.get(); - } - - default void ifAvailable(Consumer dependencyConsumer) throws BeansException { - T dependency = this.getIfAvailable(); - if (dependency != null) { - dependencyConsumer.accept(dependency); - } - - } - - @Nullable - T getIfUnique() throws BeansException; - - default T getIfUnique(Supplier defaultSupplier) throws BeansException { - T dependency = this.getIfUnique(); - return dependency != null ? dependency : defaultSupplier.get(); - } - - default void ifUnique(Consumer dependencyConsumer) throws BeansException { - T dependency = this.getIfUnique(); - if (dependency != null) { - dependencyConsumer.accept(dependency); - } - - } - - default Iterator iterator() { - return this.stream().iterator(); - } - - default Stream stream() { - throw new UnsupportedOperationException("Multi element access not supported"); - } - - default Stream orderedStream() { - throw new UnsupportedOperationException("Ordered element access not supported"); - } -} \ No newline at end of file diff --git a/bean-search-solon-plugin/pom.xml b/bean-searcher-solon-plugin/pom.xml similarity index 94% rename from bean-search-solon-plugin/pom.xml rename to bean-searcher-solon-plugin/pom.xml index 487e5ff5..123a8357 100644 --- a/bean-search-solon-plugin/pom.xml +++ b/bean-searcher-solon-plugin/pom.xml @@ -5,7 +5,7 @@ 4.0.0 Bean Searcher - bean-search-solon-plugin + bean-searcher-solon-plugin jar diff --git a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java b/bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java similarity index 68% rename from bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java rename to bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java index befc3345..8b48be55 100644 --- a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java +++ b/bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java @@ -12,8 +12,8 @@ import cn.zhxu.bs.group.GroupResolver; import cn.zhxu.bs.solon.BeanSearcherProperties.Sql; import cn.zhxu.bs.convertor.*; import cn.zhxu.bs.implement.*; -import cn.zhxu.bs.solon.beans.ObjectProvider; import cn.zhxu.bs.util.LRUCache; +import cn.zhxu.xjson.JsonKit; import org.noear.solon.annotation.Bean; import org.noear.solon.annotation.Condition; import org.noear.solon.annotation.Configuration; @@ -22,13 +22,11 @@ import org.noear.solon.core.AopContext; import javax.sql.DataSource; import java.time.ZoneId; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.function.Consumer; + @Configuration public class BeanSearcherAutoConfiguration { @@ -36,11 +34,8 @@ public class BeanSearcherAutoConfiguration { AopContext context; @Bean + @Condition(onMissingBean = PageExtractor.class) public PageExtractor pageExtractor(BeanSearcherProperties config) { - if(context.hasWrap(PageExtractor.class)){ - return null; - } - BeanSearcherProperties.Params.Pagination conf = config.getParams().getPagination(); String type = conf.getType(); BasePageExtractor extractor; @@ -48,8 +43,7 @@ public class BeanSearcherAutoConfiguration { PageSizeExtractor p = new PageSizeExtractor(); p.setPageName(conf.getPage()); extractor = p; - } else - if (BeanSearcherProperties.Params.Pagination.TYPE_OFFSET.equals(type)) { + } else if (BeanSearcherProperties.Params.Pagination.TYPE_OFFSET.equals(type)) { PageOffsetExtractor p = new PageOffsetExtractor(); p.setOffsetName(conf.getOffset()); extractor = p; @@ -77,12 +71,9 @@ public class BeanSearcherAutoConfiguration { } @Bean + @Condition(onMissingBean = Dialect.class) public Dialect dialect(BeanSearcherProperties config) { - if(context.hasWrap(Dialect.class)){ - return null; - } - - BeanSearcherProperties.Sql.Dialect dialect = config.getSql().getDialect(); + Sql.Dialect dialect = config.getSql().getDialect(); if (dialect == null) { throw new IllegalConfigException("Invalid config: [bean-searcher.sql.dialect] can not be null."); } @@ -101,10 +92,9 @@ public class BeanSearcherAutoConfiguration { } @Bean - public FieldOpPool fieldOpPool(Dialect dialect, ObjectProvider> fieldOps) { - if(context.hasWrap(FieldOpPool.class)){ - return null; - } + @Condition(onMissingBean = FieldOpPool.class) + public FieldOpPool fieldOpPool(Dialect dialect) { + List fieldOps = context.getBeansOfType(FieldOp.class); FieldOpPool pool = new FieldOpPool(); ifAvailable(fieldOps, ops -> ops.forEach(pool::addFieldOp)); @@ -113,20 +103,14 @@ public class BeanSearcherAutoConfiguration { } @Bean + @Condition(onMissingBean = ExprParser.Factory.class) public ExprParser.Factory parserFactory() { - if(context.hasWrap(ExprParser.Factory.class)){ - return null; - } - return new DefaultParserFactory(); } @Bean + @Condition(onMissingBean = GroupResolver.class) public GroupResolver groupResolver(BeanSearcherProperties config, ExprParser.Factory parserFactory) { - if(context.hasWrap(GroupResolver.class)){ - return null; - } - DefaultGroupResolver groupResolver = new DefaultGroupResolver(); BeanSearcherProperties.Params.Group conf = config.getParams().getGroup(); groupResolver.setEnabled(conf.isEnable()); @@ -137,71 +121,49 @@ public class BeanSearcherAutoConfiguration { } @Bean + @Condition(onMissingBean = BoolParamConvertor.class) public BoolParamConvertor boolParamConvertor() { - if(context.hasWrap(BoolParamConvertor.class)){ - return null; - } - return new BoolParamConvertor(); } @Bean + @Condition(onMissingBean = NumberParamConvertor.class) public NumberParamConvertor numberParamConvertor() { - if(context.hasWrap(NumberParamConvertor.class)){ - return null; - } - return new NumberParamConvertor(); } @Bean + @Condition(onMissingBean = DateParamConvertor.class) public DateParamConvertor dateParamConvertor() { - if(context.hasWrap(DateParamConvertor.class)){ - return null; - } - return new DateParamConvertor(); } @Bean + @Condition(onMissingBean = TimeParamConvertor.class) public TimeParamConvertor timeParamConvertor() { - if(context.hasWrap(TimeParamConvertor.class)){ - return null; - } - return new TimeParamConvertor(); } @Bean + @Condition(onMissingBean = DateTimeParamConvertor.class) public DateTimeParamConvertor dateTimeParamConvertor() { - if(context.hasWrap(DateTimeParamConvertor.class)){ - return null; - } - return new DateTimeParamConvertor(); } @Bean + @Condition(onMissingBean = SizeLimitParamFilter.class) public SizeLimitParamFilter sizeLimitParamFilter(BeanSearcherProperties config) { - if(context.hasWrap(SizeLimitParamFilter.class)){ - return null; - } - return new SizeLimitParamFilter(config.getParams().getFilter().getMaxParaMapSize()); } @Bean + @Condition(onMissingBean = ParamResolver.class) public ParamResolver paramResolver(PageExtractor pageExtractor, FieldOpPool fieldOpPool, List paramFilters, List convertors, GroupResolver groupResolver, BeanSearcherProperties config) { - - if(context.hasWrap(ParamResolver.class)){ - return null; - } - DefaultParamResolver paramResolver = new DefaultParamResolver(convertors, paramFilters); paramResolver.setPageExtractor(pageExtractor); paramResolver.setFieldOpPool(fieldOpPool); @@ -222,26 +184,21 @@ public class BeanSearcherAutoConfiguration { } @Bean + @Condition(onMissingBean = SqlResolver.class) public SqlResolver sqlResolver(Dialect dialect) { - if(context.hasWrap(SqlResolver.class)){ - return null; - } - return new DefaultSqlResolver(dialect); } @Bean - public SqlExecutor sqlExecutor(ObjectProvider dataSource, - ObjectProvider> namedDataSources, - ObjectProvider slowListener, - BeanSearcherProperties config) { - if(context.hasWrap(SqlExecutor.class)){ - return null; - } + @Condition(onMissingBean = SqlExecutor.class) + public SqlExecutor sqlExecutor(BeanSearcherProperties config) { + DataSource dataSource = context.getBean(DataSource.class); + List namedDataSources = context.getBeansOfType(NamedDataSource.class); + SqlExecutor.SlowListener slowListener = context.getBean(SqlExecutor.SlowListener.class); - DefaultSqlExecutor executor = new DefaultSqlExecutor(dataSource.getIfAvailable()); + DefaultSqlExecutor executor = new DefaultSqlExecutor(dataSource); ifAvailable(namedDataSources, ndsList -> { - for (NamedDataSource nds: ndsList) { + for (NamedDataSource nds : ndsList) { executor.setDataSource(nds.getName(), nds.getDataSource()); } }); @@ -251,42 +208,30 @@ public class BeanSearcherAutoConfiguration { } @Bean - @Condition(hasProperty = "${bean-searcher.field-convertor.use-number}=true") + @Condition(onMissingBean = NumberFieldConvertor.class, + onProperty = "${bean-searcher.field-convertor.use-number:true}=true") public NumberFieldConvertor numberFieldConvertor() { - if(context.hasWrap(NumberFieldConvertor.class)){ - return null; - } - return new NumberFieldConvertor(); } @Bean - @Condition(hasProperty = "${bean-searcher.field-convertor.use-str-num}=true") + @Condition(onMissingBean = StrNumFieldConvertor.class, + onProperty = "${bean-searcher.field-convertor.use-str-num:true}=true") public StrNumFieldConvertor strNumFieldConvertor() { - if(context.hasWrap(StrNumFieldConvertor.class)){ - return null; - } - return new StrNumFieldConvertor(); } @Bean - @Condition(hasProperty = "${bean-searcher.field-convertor.use-bool-num}=true") + @Condition(onMissingBean = BoolNumFieldConvertor.class, + onProperty = "${bean-searcher.field-convertor.use-bool-num:true}=true") public BoolNumFieldConvertor boolNumFieldConvertor() { - if(context.hasWrap(BoolNumFieldConvertor.class)){ - return null; - } - return new BoolNumFieldConvertor(); } @Bean - @Condition(hasProperty = "${bean-searcher.field-convertor.use-bool}=true") + @Condition(onMissingBean = BoolFieldConvertor.class, + onProperty = "${bean-searcher.field-convertor.use-bool:true}=true") public BoolFieldConvertor boolFieldConvertor(BeanSearcherProperties config) { - if(context.hasWrap(BoolFieldConvertor.class)){ - return null; - } - String[] falseValues = config.getFieldConvertor().getBoolFalseValues(); BoolFieldConvertor convertor = new BoolFieldConvertor(); if (falseValues != null) { @@ -296,12 +241,9 @@ public class BeanSearcherAutoConfiguration { } @Bean - @Condition(hasProperty = "${bean-searcher.field-convertor.use-date}=true") + @Condition(onMissingBean = DateFieldConvertor.class, + onProperty = "${bean-searcher.field-convertor.use-date:true}=true") public DateFieldConvertor dateFieldConvertor(BeanSearcherProperties config) { - if(context.hasWrap(DateFieldConvertor.class)){ - return null; - } - DateFieldConvertor convertor = new DateFieldConvertor(); ZoneId zoneId = config.getFieldConvertor().getZoneId(); if (zoneId != null) { @@ -311,22 +253,16 @@ public class BeanSearcherAutoConfiguration { } @Bean - @Condition(hasProperty = "${bean-searcher.field-convertor.use-time}=true") + @Condition(onMissingBean = TimeFieldConvertor.class, + onProperty = "${bean-searcher.field-convertor.use-time:true}=true") public TimeFieldConvertor timeFieldConvertor() { - if(context.hasWrap(TimeFieldConvertor.class)){ - return null; - } - return new TimeFieldConvertor(); } @Bean - @Condition(hasProperty = "${bean-searcher.field-convertor.use-enum}=true") + @Condition(onMissingBean = EnumFieldConvertor.class, + onProperty = "${bean-searcher.field-convertor.use-enum:true}=true") public EnumFieldConvertor enumFieldConvertor(BeanSearcherProperties config) { - if(context.hasWrap(EnumFieldConvertor.class)){ - return null; - } - BeanSearcherProperties.FieldConvertor conf = config.getFieldConvertor(); EnumFieldConvertor convertor = new EnumFieldConvertor(); convertor.setFailOnError(conf.isEnumFailOnError()); @@ -335,14 +271,13 @@ public class BeanSearcherAutoConfiguration { } - @Bean - @Condition(hasProperty = "${bean-searcher.field-convertor.use-list}=true") - public ListFieldConvertor listFieldConvertor(BeanSearcherProperties config, - ObjectProvider>> convertorsProvider) { - if(context.hasWrap(ListFieldConvertor.class)){ - return null; - } + @Condition(onMissingBean = ListFieldConvertor.class, + onProperty = "${bean-searcher.field-convertor.use-list:true}=true") + public ListFieldConvertor listFieldConvertor(BeanSearcherProperties config) { + List tmp = context.getBeansOfType(ListFieldConvertor.Convertor.class); + List> convertorsProvider = new ArrayList<>(); + tmp.forEach(c -> convertorsProvider.add(c)); BeanSearcherProperties.FieldConvertor conf = config.getFieldConvertor(); ListFieldConvertor convertor = new ListFieldConvertor(conf.getListItemSeparator()); @@ -351,24 +286,19 @@ public class BeanSearcherAutoConfiguration { } @Bean - public BeanReflector beanReflector(ObjectProvider> convertorsProvider) { - if(context.hasWrap(BeanReflector.class)){ - return null; - } + @Condition(onMissingBean = BeanReflector.class) + public BeanReflector beanReflector() { + List convertorsProvider = context.getBeansOfType(BFieldConvertor.class); - List convertors = convertorsProvider.getIfAvailable(); - if (convertors != null) { - return new DefaultBeanReflector(convertors); + if (convertorsProvider != null) { + return new DefaultBeanReflector(convertorsProvider); } return new DefaultBeanReflector(); } @Bean + @Condition(onMissingBean = DbMapping.class) public DbMapping dbMapping(BeanSearcherProperties config) { - if(context.hasWrap(DbMapping.class)){ - return null; - } - DefaultDbMapping mapping = new DefaultDbMapping(); Sql.DefaultMapping conf = config.getSql().getDefaultMapping(); mapping.setTablePrefix(conf.getTablePrefix()); @@ -383,10 +313,9 @@ public class BeanSearcherAutoConfiguration { } @Bean - public MetaResolver metaResolver(DbMapping dbMapping, ObjectProvider snippetResolver) { - if(context.hasWrap(MetaResolver.class)){ - return null; - } + @Condition(onMissingBean = MetaResolver.class) + public MetaResolver metaResolver(DbMapping dbMapping) { + SnippetResolver snippetResolver = context.getBean(SnippetResolver.class); DefaultMetaResolver metaResolver = new DefaultMetaResolver(dbMapping); ifAvailable(snippetResolver, metaResolver::setSnippetResolver); @@ -394,17 +323,16 @@ public class BeanSearcherAutoConfiguration { } @Bean - @Condition(hasProperty = "${bean-searcher.use-bean-searcher}=true") + @Condition(onMissingBean = BeanSearcher.class, + onProperty = "${bean-searcher.use-bean-searcher:true}=true") public BeanSearcher beanSearcher(MetaResolver metaResolver, ParamResolver paramResolver, SqlResolver sqlResolver, SqlExecutor sqlExecutor, - BeanReflector beanReflector, - ObjectProvider> interceptors, - ObjectProvider> processors) { - if(context.hasWrap(BeanSearcher.class)){ - return null; - } + BeanReflector beanReflector) { + + List interceptors = context.getBeansOfType(SqlInterceptor.class); + List processors = context.getBeansOfType(ResultFilter.class); DefaultBeanSearcher searcher = new DefaultBeanSearcher(); searcher.setMetaResolver(metaResolver); @@ -418,12 +346,9 @@ public class BeanSearcherAutoConfiguration { } @Bean - @Condition(hasProperty = "${bean-searcher.field-convertor.use-date-format}=true") + @Condition(onMissingBean = DateFormatFieldConvertor.class, + onProperty = "${bean-searcher.field-convertor.use-date-format:true}=true") public DateFormatFieldConvertor dateFormatFieldConvertor(BeanSearcherProperties config) { - if(context.hasWrap(DateFormatFieldConvertor.class)){ - return null; - } - BeanSearcherProperties.FieldConvertor conf = config.getFieldConvertor(); Map dateFormats = conf.getDateFormats(); ZoneId zoneId = conf.getZoneId(); @@ -442,41 +367,36 @@ public class BeanSearcherAutoConfiguration { } @Bean - @Condition(hasProperty = "${bean-searcher.field-convertor.use-b2-m}=true") - public B2MFieldConvertor b2mFieldConvertor(ObjectProvider> convertors) { - if(context.hasWrap(B2MFieldConvertor.class)){ - return null; - } + @Condition(onMissingBean = B2MFieldConvertor.class, + onProperty = "${bean-searcher.field-convertor.use-b2-m}=true") + public B2MFieldConvertor b2mFieldConvertor() { + List convertors = context.getBeansOfType(BFieldConvertor.class); - List list = convertors.getIfAvailable(); - if (list != null) { - return new B2MFieldConvertor(list); + if (convertors != null) { + return new B2MFieldConvertor(convertors); } return new B2MFieldConvertor(Collections.emptyList()); } - @Bean - @Condition(hasProperty = "${bean-searcher.use-map-searcher}=true") + @Bean //@Primary + @Condition(onMissingBean = MapSearcher.class, + onProperty = "${bean-searcher.use-map-searcher:true}=true") public MapSearcher mapSearcher(MetaResolver metaResolver, ParamResolver paramResolver, SqlResolver sqlResolver, - SqlExecutor sqlExecutor, - ObjectProvider> convertors, - ObjectProvider> interceptors, - ObjectProvider> resultFilters) { - - if(context.hasWrap(MapSearcher.class)){ - return null; - } + SqlExecutor sqlExecutor) { + List convertors = context.getBeansOfType(MFieldConvertor.class); + List interceptors = context.getBeansOfType(SqlInterceptor.class); + List resultFilters = context.getBeansOfType(ResultFilter.class); DefaultMapSearcher searcher = new DefaultMapSearcher(); searcher.setMetaResolver(metaResolver); searcher.setParamResolver(paramResolver); searcher.setSqlResolver(sqlResolver); searcher.setSqlExecutor(sqlExecutor); - List list = convertors.getIfAvailable(); - if (list != null) { - List newList = new ArrayList<>(list); + + if (convertors != null) { + List newList = new ArrayList<>(convertors); // 让 DateFormatFieldConvertor 排在前面 newList.sort((o1, o2) -> { if (o1 instanceof DateFormatFieldConvertor) { @@ -494,13 +414,20 @@ public class BeanSearcherAutoConfiguration { return searcher; } - private void ifAvailable(ObjectProvider provider, Consumer consumer) { - // 为了兼容 1.x 的 SpringBoot,最低兼容到 v1.4 - // 不直接使用 ObjectProvider.ifAvailable 方法 - T dependency = provider.getIfAvailable(); - if (dependency != null) { - consumer.accept(dependency); - } + // + // 在 springboot 那边,是用单独类处理的;在 solon 这边,用函数 + // + @Bean + @Condition(onClass = JsonKit.class, + onProperty = "${bean-searcher.field-convertor.use-json:true}=true") + public JsonFieldConvertor jsonFieldConvertor(BeanSearcherProperties config) { + BeanSearcherProperties.FieldConvertor conf = config.getFieldConvertor(); + return new JsonFieldConvertor(conf.isJsonFailOnError()); } -} + private void ifAvailable(T provider, Consumer consumer) { + if (provider != null) { + consumer.accept(provider); + } + } +} \ No newline at end of file diff --git a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherProperties.java b/bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherProperties.java similarity index 100% rename from bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherProperties.java rename to bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherProperties.java diff --git a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/IllegalConfigException.java b/bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/IllegalConfigException.java similarity index 100% rename from bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/IllegalConfigException.java rename to bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/IllegalConfigException.java diff --git a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/NamedDataSource.java b/bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/NamedDataSource.java similarity index 100% rename from bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/NamedDataSource.java rename to bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/NamedDataSource.java diff --git a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/integration/XPluginImpl.java b/bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/integration/XPluginImpl.java similarity index 80% rename from bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/integration/XPluginImpl.java rename to bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/integration/XPluginImpl.java index e4d66820..15530db6 100644 --- a/bean-search-solon-plugin/src/main/java/cn/zhxu/bs/solon/integration/XPluginImpl.java +++ b/bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/integration/XPluginImpl.java @@ -3,12 +3,12 @@ package cn.zhxu.bs.solon.integration; import cn.zhxu.bs.solon.BeanSearcherAutoConfiguration; import cn.zhxu.bs.solon.BeanSearcherProperties; -import cn.zhxu.bs.solon.JsonFieldConvertorConfig; import org.noear.solon.core.AopContext; import org.noear.solon.core.Plugin; /** - * @author noear 2023/2/18 created + * @author noear + * @since 2.1 */ public class XPluginImpl implements Plugin { @Override @@ -18,7 +18,6 @@ public class XPluginImpl implements Plugin { //容器加载完成后再执行,确保用户的 Bean 优先 context.beanOnloaded(x -> { x.beanMake(BeanSearcherAutoConfiguration.class); - x.beanMake(JsonFieldConvertorConfig.class); }); } } diff --git a/bean-search-solon-plugin/src/main/resources/META-INF/solon/bean-search-solon-plugin.properties b/bean-searcher-solon-plugin/src/main/resources/META-INF/solon/bean-search-solon-plugin.properties similarity index 100% rename from bean-search-solon-plugin/src/main/resources/META-INF/solon/bean-search-solon-plugin.properties rename to bean-searcher-solon-plugin/src/main/resources/META-INF/solon/bean-search-solon-plugin.properties diff --git a/pom.xml b/pom.xml index 7e5a319f..10c99ef2 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ bean-searcher - bean-search-solon-plugin + bean-searcher-solon-plugin bean-searcher-boot-starter @@ -25,7 +25,7 @@ 2.0.6 1.4.2 3.0.2 - 2.1.3 + 2.1.4-M1 UTF-8 -- Gitee From 948a28bb3ef5b966edfba633ea4dba6fd2c491cf Mon Sep 17 00:00:00 2001 From: noear Date: Sun, 19 Feb 2023 17:38:56 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AE=8C=E5=96=84=20bean-search-solon-plug?= =?UTF-8?q?in=20=E9=80=82=E9=85=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../solon/BeanSearcherAutoConfiguration.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java b/bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java index 8b48be55..ff9457a0 100644 --- a/bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java +++ b/bean-searcher-solon-plugin/src/main/java/cn/zhxu/bs/solon/BeanSearcherAutoConfiguration.java @@ -33,9 +33,13 @@ public class BeanSearcherAutoConfiguration { @Inject AopContext context; + //放到这儿,减少注入处理代码 + @Inject + BeanSearcherProperties config; + @Bean @Condition(onMissingBean = PageExtractor.class) - public PageExtractor pageExtractor(BeanSearcherProperties config) { + public PageExtractor pageExtractor() { BeanSearcherProperties.Params.Pagination conf = config.getParams().getPagination(); String type = conf.getType(); BasePageExtractor extractor; @@ -72,7 +76,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(onMissingBean = Dialect.class) - public Dialect dialect(BeanSearcherProperties config) { + public Dialect dialect() { Sql.Dialect dialect = config.getSql().getDialect(); if (dialect == null) { throw new IllegalConfigException("Invalid config: [bean-searcher.sql.dialect] can not be null."); @@ -110,7 +114,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(onMissingBean = GroupResolver.class) - public GroupResolver groupResolver(BeanSearcherProperties config, ExprParser.Factory parserFactory) { + public GroupResolver groupResolver(ExprParser.Factory parserFactory) { DefaultGroupResolver groupResolver = new DefaultGroupResolver(); BeanSearcherProperties.Params.Group conf = config.getParams().getGroup(); groupResolver.setEnabled(conf.isEnable()); @@ -152,7 +156,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(onMissingBean = SizeLimitParamFilter.class) - public SizeLimitParamFilter sizeLimitParamFilter(BeanSearcherProperties config) { + public SizeLimitParamFilter sizeLimitParamFilter() { return new SizeLimitParamFilter(config.getParams().getFilter().getMaxParaMapSize()); } @@ -162,8 +166,7 @@ public class BeanSearcherAutoConfiguration { FieldOpPool fieldOpPool, List paramFilters, List convertors, - GroupResolver groupResolver, - BeanSearcherProperties config) { + GroupResolver groupResolver) { DefaultParamResolver paramResolver = new DefaultParamResolver(convertors, paramFilters); paramResolver.setPageExtractor(pageExtractor); paramResolver.setFieldOpPool(fieldOpPool); @@ -191,7 +194,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(onMissingBean = SqlExecutor.class) - public SqlExecutor sqlExecutor(BeanSearcherProperties config) { + public SqlExecutor sqlExecutor() { DataSource dataSource = context.getBean(DataSource.class); List namedDataSources = context.getBeansOfType(NamedDataSource.class); SqlExecutor.SlowListener slowListener = context.getBean(SqlExecutor.SlowListener.class); @@ -231,7 +234,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(onMissingBean = BoolFieldConvertor.class, onProperty = "${bean-searcher.field-convertor.use-bool:true}=true") - public BoolFieldConvertor boolFieldConvertor(BeanSearcherProperties config) { + public BoolFieldConvertor boolFieldConvertor() { String[] falseValues = config.getFieldConvertor().getBoolFalseValues(); BoolFieldConvertor convertor = new BoolFieldConvertor(); if (falseValues != null) { @@ -243,7 +246,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(onMissingBean = DateFieldConvertor.class, onProperty = "${bean-searcher.field-convertor.use-date:true}=true") - public DateFieldConvertor dateFieldConvertor(BeanSearcherProperties config) { + public DateFieldConvertor dateFieldConvertor() { DateFieldConvertor convertor = new DateFieldConvertor(); ZoneId zoneId = config.getFieldConvertor().getZoneId(); if (zoneId != null) { @@ -262,7 +265,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(onMissingBean = EnumFieldConvertor.class, onProperty = "${bean-searcher.field-convertor.use-enum:true}=true") - public EnumFieldConvertor enumFieldConvertor(BeanSearcherProperties config) { + public EnumFieldConvertor enumFieldConvertor() { BeanSearcherProperties.FieldConvertor conf = config.getFieldConvertor(); EnumFieldConvertor convertor = new EnumFieldConvertor(); convertor.setFailOnError(conf.isEnumFailOnError()); @@ -274,7 +277,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(onMissingBean = ListFieldConvertor.class, onProperty = "${bean-searcher.field-convertor.use-list:true}=true") - public ListFieldConvertor listFieldConvertor(BeanSearcherProperties config) { + public ListFieldConvertor listFieldConvertor() { List tmp = context.getBeansOfType(ListFieldConvertor.Convertor.class); List> convertorsProvider = new ArrayList<>(); tmp.forEach(c -> convertorsProvider.add(c)); @@ -298,7 +301,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(onMissingBean = DbMapping.class) - public DbMapping dbMapping(BeanSearcherProperties config) { + public DbMapping dbMapping() { DefaultDbMapping mapping = new DefaultDbMapping(); Sql.DefaultMapping conf = config.getSql().getDefaultMapping(); mapping.setTablePrefix(conf.getTablePrefix()); @@ -348,7 +351,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(onMissingBean = DateFormatFieldConvertor.class, onProperty = "${bean-searcher.field-convertor.use-date-format:true}=true") - public DateFormatFieldConvertor dateFormatFieldConvertor(BeanSearcherProperties config) { + public DateFormatFieldConvertor dateFormatFieldConvertor() { BeanSearcherProperties.FieldConvertor conf = config.getFieldConvertor(); Map dateFormats = conf.getDateFormats(); ZoneId zoneId = conf.getZoneId(); @@ -420,7 +423,7 @@ public class BeanSearcherAutoConfiguration { @Bean @Condition(onClass = JsonKit.class, onProperty = "${bean-searcher.field-convertor.use-json:true}=true") - public JsonFieldConvertor jsonFieldConvertor(BeanSearcherProperties config) { + public JsonFieldConvertor jsonFieldConvertor() { BeanSearcherProperties.FieldConvertor conf = config.getFieldConvertor(); return new JsonFieldConvertor(conf.isJsonFailOnError()); } -- Gitee