- fix: 多包scan及enable false 无效问题
This commit is contained in:
xpc 2025-03-27 21:46:45 +08:00
parent 7bcf9dda2c
commit ae0a309087
22 changed files with 88 additions and 48 deletions

View File

@ -7,7 +7,7 @@
<parent>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-parent</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
<relativePath>../easy-es-parent</relativePath>
</parent>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-parent</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
<relativePath>../easy-es-parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -4,13 +4,16 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient;
import org.dromara.easyes.common.property.EasyEsDynamicProperties;
import org.dromara.easyes.common.property.EasyEsProperties;
import org.dromara.easyes.common.utils.EsClientUtils;
import org.dromara.easyes.spring.config.EasyEsConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.Ordered;
/**
* es自动配置
@ -19,21 +22,10 @@ import org.springframework.context.annotation.Configuration;
**/
@Configuration
@ConditionalOnClass(ElasticsearchClient.class)
@ConditionalOnExpression("'${easy-es.address:x}'!='x'")
@ConditionalOnProperty(prefix = "easy-es", name = {"enable"}, havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(prefix = "easy-es", name = "enable", havingValue = "true", matchIfMissing = true)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) // 确保配置类优先加载
public class EsAutoConfiguration {
/**
* 装配ElasticsearchClient
*
* @return ElasticsearchClient bean
*/
@Bean
@ConditionalOnMissingBean
public ElasticsearchClient elasticClient() {
return EsClientUtils.buildClient(easyEsProperties());
}
@Bean
@ConfigurationProperties(prefix = "easy-es")
public EasyEsProperties easyEsProperties() {
@ -46,4 +38,16 @@ public class EsAutoConfiguration {
return new EasyEsDynamicProperties();
}
@Bean
@ConditionalOnMissingBean
@DependsOn({"easyEsProperties", "easyEsDynamicProperties"}) // 显式依赖
public ElasticsearchClient elasticClient() {
return EsClientUtils.buildClient(easyEsProperties());
}
@Bean
public EasyEsConfiguration esConfiguration() {
return new EasyEsConfiguration();
}
}

View File

@ -163,7 +163,7 @@
"type": "java.lang.Boolean"
},
{
"name": "easy-es.dynamic",
"name": "easy-es.dynamic.datasource",
"description": "Whether to intelligently add the. keyword suffix to the field. This configuration is enabled by default. The field type is KEYWORD only for annotation configuration_ The String field of TEXT or unconfigured type takes effect and only takes effect when the query requires that the field be of keyword type, so it is called smart! 是否智能为字段添加.keyword后缀 默认开启 此配置仅对注解配置字段类型为KEYWORD_TEXT或未配置类型的String字段生效并且只会在查询要求该字段必须为keyword类型的查询中才生效因此谓之智能!.",
"type": "java.util.Map<java.lang.String, org.dromara.easyes.common.property.EasyEsProperties>"
}

View File

@ -1,5 +1,4 @@
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.dromara.easyes.starter.EsAutoConfiguration,\
org.dromara.easyes.spring.config.EasyEsConfiguration,\
org.dromara.easyes.starter.config.GeneratorConfiguration

View File

@ -6,7 +6,7 @@
<parent>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-parent</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
<relativePath>../easy-es-parent</relativePath>
</parent>

View File

@ -7,7 +7,7 @@
<parent>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-parent</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
<relativePath>../easy-es-parent</relativePath>
</parent>

View File

@ -7,7 +7,7 @@
<parent>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-parent</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
<relativePath>../easy-es-parent</relativePath>
</parent>

View File

@ -6,7 +6,7 @@
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-parent</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
<name>easy-es-parent</name>
<description>easy use for elastic search</description>

View File

@ -3,7 +3,7 @@
<parent>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-parent</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
<relativePath>../easy-es-parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -21,7 +21,7 @@ import java.util.Map;
* Copyright © 2021 xpc1024 All Rights Reserved
**/
@Configuration
@Condition(onClass = ElasticsearchClient.class, onProperty = "${easy-es.enable:true} = true && ${easy-es.address:x} != x")
@Condition(onClass = ElasticsearchClient.class, onProperty = "${easy-es.enable:true} = true")
public class EsAutoConfiguration {
@Bean

View File

@ -118,6 +118,12 @@ public class MapperScannerRegister implements BeanBuilder<EsMapperScan> {
}
// 查找es mapper
for (String doScan : anno.value()) {
// 判断是否需要处理${}变量
if (doScan.contains("${") && doScan.contains("}")) {
String basePackage = Solon.cfg().getByTmpl(doScan);
LogUtils.formatInfo("Scan Easy-Es Mapper[%s -> %s]", doScan, basePackage);
doScan = basePackage;
}
Collection<Class<?>> classPath = ClassUtil.scanClasses(doScan);
for (Class<?> clazz : classPath) {
// 跳过非ee的mapper,比如瞎几把写的接口,没有继承BaseEsMapper继承了的推入容器

View File

@ -4,7 +4,7 @@
<parent>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
</parent>
<artifactId>easy-es-solon-test</artifactId>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
</parent>
<artifactId>easy-es-spring-test</artifactId>

View File

@ -6,7 +6,7 @@
<parent>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-parent</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
<relativePath>../easy-es-parent</relativePath>
</parent>

View File

@ -67,12 +67,13 @@ public class MapperScannerConfigurer
// 取变量
this.basePackage = Optional.ofNullable(this.basePackage)
.map(getEnvironment()::resolvePlaceholders).orElse(null);
.map(getEnvironment()::resolvePlaceholders).orElse(EMPTY_STR);
// 做扫包的操作了与注解扫包类似只不过这里是spring配置方式
// 在mybatis中配置了很多扫描拦截属性这里放到后面拓展
ClassPathMapperScanner scanner = new ClassPathMapperScanner(beanDefinitionRegistry, getEnvironment());
scanner.registerFilters();
scanner.doScan(this.basePackage);
String[] packages = this.basePackage.split(COMMA);
scanner.doScan(packages);
}
private String getPropertyValue(String propertyName, PropertyValues values) {

View File

@ -1,11 +1,14 @@
package org.dromara.easyes.spring.annotation;
import org.dromara.easyes.common.utils.LogUtils;
import org.dromara.easyes.spring.MapperScannerConfigurer;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;
@ -13,12 +16,20 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.dromara.easyes.common.constants.BaseEsConstants.COMMA;
/**
* 注册bean
* <p>
* Copyright © 2021 xpc1024 All Rights Reserved
**/
public class MapperScannerRegister implements ImportBeanDefinitionRegistrar {
public class MapperScannerRegister implements ImportBeanDefinitionRegistrar, EnvironmentAware {
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
@ -27,9 +38,19 @@ public class MapperScannerRegister implements ImportBeanDefinitionRegistrar {
// 默认已注解标记为主如果没有则尝试寻找easy-es配置 scan
if (mapperScanAttrs != null) {
List<String> basePackages = Arrays.stream(mapperScanAttrs.getStringArray("value"))
.filter(StringUtils::hasText).collect(Collectors.toList());
.filter(StringUtils::hasText)
.map(map -> {
// 判断是否需要处理${}变量
if (map.contains("${") && map.contains("}")) {
String basePackage = this.environment.resolvePlaceholders(map);
LogUtils.formatInfo("Scan Easy-Es Mapper[%s -> %s]", map, basePackage);
return basePackage;
}
return map;
})
.collect(Collectors.toList());
// 注册bean
registerBeanDefinitions(registry, generateBaseBeanName(importingClassMetadata, 0),
registerBeanDefinitions(registry, generateBaseBeanName(importingClassMetadata),
StringUtils.toStringArray(basePackages));
}
}
@ -38,19 +59,20 @@ public class MapperScannerRegister implements ImportBeanDefinitionRegistrar {
* 通过BeanDefinition注册bean在bean注册前处理interface中bean参数问题interface与普通bean不同
* 使用了jdk动态代理需要确定注册的实际interface class就需要通过BeanDefinition来追加属性
* 当前使用到了spring的构造
*
* @param registry spring bean扫码注册器
* @param basePackages 扫码的包
*/
void registerBeanDefinitions(BeanDefinitionRegistry registry, String beanName, String... basePackages) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
builder.addPropertyValue("basePackage", String.join(",", basePackages));
builder.addPropertyValue("basePackage", String.join(COMMA, basePackages));
// for spring-native
builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(beanName, builder.getBeanDefinition());
}
private static String generateBaseBeanName(AnnotationMetadata importingClassMetadata, int index) {
return importingClassMetadata.getClassName() + "#" + MapperScannerRegister.class.getSimpleName() + "#" + index;
private static String generateBaseBeanName(AnnotationMetadata importingClassMetadata) {
return importingClassMetadata.getClassName() + "#" + MapperScannerRegister.class.getSimpleName() + "#" + 0;
}
}

View File

@ -1,5 +1,6 @@
package org.dromara.easyes.spring.config;
import lombok.NonNull;
import lombok.Setter;
import org.dromara.easyes.common.property.EasyEsDynamicProperties;
import org.dromara.easyes.common.property.EasyEsProperties;
@ -10,8 +11,10 @@ import org.dromara.easyes.core.index.AutoProcessIndexSmoothlyStrategy;
import org.dromara.easyes.spring.factory.IndexStrategyFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import java.util.Map;
@ -22,22 +25,27 @@ import java.util.Map;
*/
@Setter
@Configuration
public class EasyEsConfiguration implements InitializingBean {
public class EasyEsConfiguration implements InitializingBean, EnvironmentAware {
private Environment environment;
@Autowired(required = false)
private EasyEsProperties easyEsProperties;
@Autowired(required = false)
private EasyEsDynamicProperties easyEsDynamicProperties;
@Autowired
public EasyEsConfiguration(EasyEsProperties easyEsProperties, EasyEsDynamicProperties easyEsDynamicProperties) {
this.easyEsProperties = easyEsProperties;
this.easyEsDynamicProperties = easyEsDynamicProperties;
@Override
public void setEnvironment(@NonNull Environment environment) {
this.environment = environment;
}
@Override
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
Boolean enable = environment.getProperty("easy-es.enable", Boolean.class, Boolean.TRUE);
if (enable) {
Assert.notNull(this.easyEsProperties, "easyEsProperties must is A bean. easy-es配置类必须给配置一个bean");
}
}
@Bean
public IndexStrategyFactory indexStrategyFactory() {

View File

@ -9,7 +9,7 @@
<parent>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
</parent>
<properties>

View File

@ -7,7 +7,7 @@
<parent>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
</parent>
<artifactId>easy-es-springboot-test</artifactId>

View File

@ -52,7 +52,7 @@ public class TenantLineInnerInterceptor implements Interceptor {
// wrapper.eq("tenantId", "1");
// return invocation.proceed();
// }
System.out.println("增则拦截方法");
System.out.println("easy-es intercept...");
return invocation.proceed();
}
}

View File

@ -6,7 +6,7 @@
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es</artifactId>
<version>2.1.0</version>
<version>3.0.0</version>
<name>easy-es</name>
<description>easy use for elastic search</description>