From c2f7dece35ae8d5b96519cfbe54904630ed0d0c8 Mon Sep 17 00:00:00 2001 From: luozhenyu Date: Sat, 27 Apr 2024 19:55:58 +0800 Subject: [PATCH] Add environment to ClassPathMapperScanner to get properties from PropertySources --- pom.xml | 8 ++++++ .../spring/mapper/ClassPathMapperScanner.java | 13 ++++++++- .../mapper/MapperScannerConfigurer.java | 4 +-- .../AnnotatedMapperOnPropertyCondition.java | 27 +++++++++++++++++++ .../mapper/MapperScannerConfigurerTest.java | 16 ++++++++++- 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/mybatis/spring/mapper/AnnotatedMapperOnPropertyCondition.java diff --git a/pom.xml b/pom.xml index dffc1301b2..afb3b778bb 100644 --- a/pom.xml +++ b/pom.xml @@ -113,6 +113,7 @@ 10.17.1.0 3.5.16 6.1.6 + 3.2.5 5.1.1 org.mybatis.spring @@ -218,6 +219,13 @@ + + org.springframework.boot + spring-boot-autoconfigure + ${spring-boot.version} + test + + org.hsqldb hsqldb diff --git a/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java b/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java index fab1c66ac5..dca8d02f3f 100644 --- a/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java +++ b/src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2023 the original author or authors. + * Copyright 2010-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,6 +38,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; import org.springframework.core.NativeDetector; +import org.springframework.core.env.Environment; import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.core.type.filter.AssignableTypeFilter; import org.springframework.util.StringUtils; @@ -86,6 +87,16 @@ public class ClassPathMapperScanner extends ClassPathBeanDefinitionScanner { private String defaultScope; + public ClassPathMapperScanner(BeanDefinitionRegistry registry, Environment environment) { + super(registry, false, environment); + setIncludeAnnotationConfig(!AotDetector.useGeneratedArtifacts()); + setPrintWarnLogIfNotFoundMappers(!NativeDetector.inNativeImage()); + } + + /** + * @deprecated Please use the {@link #ClassPathMapperScanner(BeanDefinitionRegistry, Environment)}. + */ + @Deprecated(since = "3.0.4", forRemoval = true) public ClassPathMapperScanner(BeanDefinitionRegistry registry) { super(registry, false); setIncludeAnnotationConfig(!AotDetector.useGeneratedArtifacts()); diff --git a/src/main/java/org/mybatis/spring/mapper/MapperScannerConfigurer.java b/src/main/java/org/mybatis/spring/mapper/MapperScannerConfigurer.java index 5b10b71192..29552ef25b 100644 --- a/src/main/java/org/mybatis/spring/mapper/MapperScannerConfigurer.java +++ b/src/main/java/org/mybatis/spring/mapper/MapperScannerConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2022 the original author or authors. + * Copyright 2010-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -360,7 +360,7 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { processPropertyPlaceHolders(); } - ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry); + ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry, getEnvironment()); scanner.setAddToConfig(this.addToConfig); scanner.setAnnotationClass(this.annotationClass); scanner.setMarkerInterface(this.markerInterface); diff --git a/src/test/java/org/mybatis/spring/mapper/AnnotatedMapperOnPropertyCondition.java b/src/test/java/org/mybatis/spring/mapper/AnnotatedMapperOnPropertyCondition.java new file mode 100644 index 0000000000..a5170a92bc --- /dev/null +++ b/src/test/java/org/mybatis/spring/mapper/AnnotatedMapperOnPropertyCondition.java @@ -0,0 +1,27 @@ +/* + * Copyright 2010-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.spring.mapper; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; + +// annotated interface for MapperScannerPostProcessor tests +// ensures annotated classes are loaded on property condition +@Component +@ConditionalOnProperty(prefix = "mapper", value = "condition", havingValue = "true") +public interface AnnotatedMapperOnPropertyCondition { + void method(); +} diff --git a/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java b/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java index 3a9a3f16f9..fc1ce5f4b3 100644 --- a/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java +++ b/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2023 the original author or authors. + * Copyright 2010-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,8 @@ import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.SimpleThreadScope; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.mock.env.MockPropertySource; import org.springframework.stereotype.Component; class MapperScannerConfigurerTest { @@ -402,6 +404,18 @@ void testMapperBeanAttribute() { .getAttribute(ClassPathMapperScanner.FACTORY_BEAN_OBJECT_TYPE)).isEqualTo(AnnotatedMapper.class); } + @Test + void testMapperBeanOnConditionalProperties() { + MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources(); + propertySources.addLast(new MockPropertySource().withProperty("mapper.condition", "true")); + + startContext(); + + assertThat(applicationContext.getBeanDefinition("annotatedMapperOnPropertyCondition") + .getAttribute(ClassPathMapperScanner.FACTORY_BEAN_OBJECT_TYPE)) + .isEqualTo(AnnotatedMapperOnPropertyCondition.class); + } + private void setupSqlSessionFactory(String name) { GenericBeanDefinition definition = new GenericBeanDefinition(); definition.setBeanClass(SqlSessionFactoryBean.class);