Skip to content

Commit afd3335

Browse files
authored
Merge pull request #939 from luozhenyu/property-sources
Add environment to ClassPathMapperScanner to get properties from PropertySources
2 parents 126977c + c2f7dec commit afd3335

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
<derby.version>10.17.1.0</derby.version>
114114
<mybatis.version>3.5.16</mybatis.version>
115115
<spring.version>6.1.6</spring.version>
116+
<spring-boot.version>3.2.5</spring-boot.version>
116117
<spring-batch.version>5.1.1</spring-batch.version>
117118
<module.name>org.mybatis.spring</module.name>
118119

@@ -218,6 +219,13 @@
218219
</exclusions>
219220
</dependency>
220221

222+
<dependency>
223+
<groupId>org.springframework.boot</groupId>
224+
<artifactId>spring-boot-autoconfigure</artifactId>
225+
<version>${spring-boot.version}</version>
226+
<scope>test</scope>
227+
</dependency>
228+
221229
<dependency>
222230
<groupId>org.hsqldb</groupId>
223231
<artifactId>hsqldb</artifactId>

src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 the original author or authors.
2+
* Copyright 2010-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@
3838
import org.springframework.beans.factory.support.RootBeanDefinition;
3939
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
4040
import org.springframework.core.NativeDetector;
41+
import org.springframework.core.env.Environment;
4142
import org.springframework.core.type.filter.AnnotationTypeFilter;
4243
import org.springframework.core.type.filter.AssignableTypeFilter;
4344
import org.springframework.util.StringUtils;
@@ -86,6 +87,16 @@ public class ClassPathMapperScanner extends ClassPathBeanDefinitionScanner {
8687

8788
private String defaultScope;
8889

90+
public ClassPathMapperScanner(BeanDefinitionRegistry registry, Environment environment) {
91+
super(registry, false, environment);
92+
setIncludeAnnotationConfig(!AotDetector.useGeneratedArtifacts());
93+
setPrintWarnLogIfNotFoundMappers(!NativeDetector.inNativeImage());
94+
}
95+
96+
/**
97+
* @deprecated Please use the {@link #ClassPathMapperScanner(BeanDefinitionRegistry, Environment)}.
98+
*/
99+
@Deprecated(since = "3.0.4", forRemoval = true)
89100
public ClassPathMapperScanner(BeanDefinitionRegistry registry) {
90101
super(registry, false);
91102
setIncludeAnnotationConfig(!AotDetector.useGeneratedArtifacts());

src/main/java/org/mybatis/spring/mapper/MapperScannerConfigurer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2022 the original author or authors.
2+
* Copyright 2010-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -360,7 +360,7 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
360360
processPropertyPlaceHolders();
361361
}
362362

363-
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
363+
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry, getEnvironment());
364364
scanner.setAddToConfig(this.addToConfig);
365365
scanner.setAnnotationClass(this.annotationClass);
366366
scanner.setMarkerInterface(this.markerInterface);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2010-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.spring.mapper;
17+
18+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
19+
import org.springframework.stereotype.Component;
20+
21+
// annotated interface for MapperScannerPostProcessor tests
22+
// ensures annotated classes are loaded on property condition
23+
@Component
24+
@ConditionalOnProperty(prefix = "mapper", value = "condition", havingValue = "true")
25+
public interface AnnotatedMapperOnPropertyCondition {
26+
void method();
27+
}

src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 the original author or authors.
2+
* Copyright 2010-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,8 @@
4646
import org.springframework.beans.factory.support.GenericBeanDefinition;
4747
import org.springframework.context.support.GenericApplicationContext;
4848
import org.springframework.context.support.SimpleThreadScope;
49+
import org.springframework.core.env.MutablePropertySources;
50+
import org.springframework.mock.env.MockPropertySource;
4951
import org.springframework.stereotype.Component;
5052

5153
class MapperScannerConfigurerTest {
@@ -402,6 +404,18 @@ void testMapperBeanAttribute() {
402404
.getAttribute(ClassPathMapperScanner.FACTORY_BEAN_OBJECT_TYPE)).isEqualTo(AnnotatedMapper.class);
403405
}
404406

407+
@Test
408+
void testMapperBeanOnConditionalProperties() {
409+
MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
410+
propertySources.addLast(new MockPropertySource().withProperty("mapper.condition", "true"));
411+
412+
startContext();
413+
414+
assertThat(applicationContext.getBeanDefinition("annotatedMapperOnPropertyCondition")
415+
.getAttribute(ClassPathMapperScanner.FACTORY_BEAN_OBJECT_TYPE))
416+
.isEqualTo(AnnotatedMapperOnPropertyCondition.class);
417+
}
418+
405419
private void setupSqlSessionFactory(String name) {
406420
GenericBeanDefinition definition = new GenericBeanDefinition();
407421
definition.setBeanClass(SqlSessionFactoryBean.class);

0 commit comments

Comments
 (0)