Skip to content

Commit 76e8bae

Browse files
committed
Support processPropertyPlaceHolders option in mapper:scan and @MapperScan
Fixes gh-829
1 parent 9507fe0 commit 76e8bae

15 files changed

+380
-8
lines changed

src/main/java/org/mybatis/spring/annotation/MapperScan.java

Lines changed: 12 additions & 1 deletion
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-2023 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.
@@ -185,4 +185,15 @@
185185
*/
186186
String defaultScope() default AbstractBeanDefinition.SCOPE_DEFAULT;
187187

188+
/**
189+
* Specifies a flag that whether execute a property placeholder processing or not.
190+
* <p>
191+
* The default is {@literal true}. This means that a property placeholder processing execute.
192+
*
193+
* @since 3.0.3
194+
*
195+
* @return a flag that whether execute a property placeholder processing or not
196+
*/
197+
boolean processPropertyPlaceHolders() default true;
198+
188199
}

src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java

Lines changed: 2 additions & 2 deletions
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-2023 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.
@@ -82,7 +82,7 @@ void registerBeanDefinitions(AnnotationMetadata annoMeta, AnnotationAttributes a
8282
BeanDefinitionRegistry registry, String beanName) {
8383

8484
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
85-
builder.addPropertyValue("processPropertyPlaceHolders", true);
85+
builder.addPropertyValue("processPropertyPlaceHolders", annoAttrs.getBoolean("processPropertyPlaceHolders"));
8686

8787
Class<? extends Annotation> annotationClass = annoAttrs.getClass("annotationClass");
8888
if (!Annotation.class.equals(annotationClass)) {

src/main/java/org/mybatis/spring/config/MapperScannerBeanDefinitionParser.java

Lines changed: 5 additions & 2 deletions
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-2023 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.
@@ -56,6 +56,7 @@ public class MapperScannerBeanDefinitionParser extends AbstractBeanDefinitionPar
5656
private static final String ATTRIBUTE_MAPPER_FACTORY_BEAN_CLASS = "mapper-factory-bean-class";
5757
private static final String ATTRIBUTE_LAZY_INITIALIZATION = "lazy-initialization";
5858
private static final String ATTRIBUTE_DEFAULT_SCOPE = "default-scope";
59+
private static final String ATTRIBUTE_PROCESS_PROPERTY_PLACEHOLDERS = "process-property-placeholders";
5960

6061
/**
6162
* {@inheritDoc}
@@ -68,7 +69,9 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
6869

6970
ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
7071

71-
builder.addPropertyValue("processPropertyPlaceHolders", true);
72+
String processPropertyPlaceHolders = element.getAttribute(ATTRIBUTE_PROCESS_PROPERTY_PLACEHOLDERS);
73+
builder.addPropertyValue("processPropertyPlaceHolders",
74+
!StringUtils.hasText(processPropertyPlaceHolders) || Boolean.parseBoolean(processPropertyPlaceHolders));
7275
try {
7376
String annotationClassName = element.getAttribute(ATTRIBUTE_ANNOTATION);
7477
if (StringUtils.hasText(annotationClassName)) {

src/main/resources/org/mybatis/spring/config/mybatis-spring.xsd

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2010-2022 the original author or authors.
4+
Copyright 2010-2023 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -148,6 +148,16 @@
148148
</xsd:documentation>
149149
</xsd:annotation>
150150
</xsd:attribute>
151+
<xsd:attribute name="process-property-placeholders" type="xsd:boolean">
152+
<xsd:annotation>
153+
<xsd:documentation>
154+
<![CDATA[
155+
Specifies a flag that whether execute a property placeholder processing or not. (Since 3.0.3)
156+
The default is true. This means that a property placeholder processing execute.
157+
]]>
158+
</xsd:documentation>
159+
</xsd:annotation>
160+
</xsd:attribute>
151161
</xsd:complexType>
152162
</xsd:element>
153163
</xsd:schema>

src/test/java/org/mybatis/spring/annotation/MapperScanTest.java

Lines changed: 75 additions & 1 deletion
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-2023 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,7 @@
4646
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
4747
import org.springframework.beans.factory.config.BeanDefinition;
4848
import org.springframework.beans.factory.config.ConstructorArgumentValues;
49+
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
4950
import org.springframework.beans.factory.config.RuntimeBeanReference;
5051
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
5152
import org.springframework.beans.factory.support.GenericBeanDefinition;
@@ -390,6 +391,32 @@ void testScopedProxyMapperScanByDefaultScope() {
390391
assertEquals(2, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());
391392
}
392393

394+
@Test
395+
void testProcessPropertyPlaceHoldersIsTrue() {
396+
applicationContext.register(ProcessPropertyPlaceHoldersTrueConfiguration.class);
397+
398+
startContext();
399+
400+
SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
401+
assertEquals(1, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());
402+
403+
MyBean myBean = applicationContext.getBean(MyBean.class);
404+
assertThat(myBean.getName()).isEqualTo("MyBean!!");
405+
}
406+
407+
@Test
408+
void testProcessPropertyPlaceHoldersIsFalse() {
409+
applicationContext.register(ProcessPropertyPlaceHoldersFalseConfiguration.class);
410+
411+
startContext();
412+
413+
SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
414+
assertEquals(1, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());
415+
416+
MyBean myBean = applicationContext.getBean(MyBean.class);
417+
assertThat(myBean.getName()).isEqualTo("MyBean!!");
418+
}
419+
393420
@Configuration
394421
@MapperScan("org.mybatis.spring.mapper")
395422
public static class AppConfigWithPackageScan {
@@ -459,6 +486,53 @@ static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer
459486

460487
}
461488

489+
@ComponentScan("org.mybatis.spring.annotation.factory")
490+
@MapperScan(basePackages = "${scan-package}")
491+
public static class ProcessPropertyPlaceHoldersTrueConfiguration {
492+
@Bean
493+
static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
494+
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
495+
configurer.setLocation(new ClassPathResource("/org/mybatis/spring/annotation/placeholders.properties"));
496+
return configurer;
497+
}
498+
499+
@Bean
500+
static PropertyOverrideConfigurer propertyOverrideConfigurer() {
501+
PropertyOverrideConfigurer configurer = new PropertyOverrideConfigurer();
502+
configurer.setLocation(new ClassPathResource("/org/mybatis/spring/annotation/override.properties"));
503+
configurer.setIgnoreInvalidKeys(true);
504+
return configurer;
505+
}
506+
507+
@Bean
508+
MyBean myBean() {
509+
return new MyBean("annotation");
510+
}
511+
}
512+
513+
@ComponentScan("org.mybatis.spring.annotation.factory")
514+
@MapperScan(basePackages = "org.mybatis.spring.annotation.mapper.ds1", processPropertyPlaceHolders = false)
515+
public static class ProcessPropertyPlaceHoldersFalseConfiguration {
516+
@Bean
517+
static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
518+
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
519+
configurer.setLocation(new ClassPathResource("/org/mybatis/spring/annotation/placeholders.properties"));
520+
return configurer;
521+
}
522+
523+
@Bean
524+
static PropertyOverrideConfigurer propertyOverrideConfigurer() {
525+
PropertyOverrideConfigurer configurer = new PropertyOverrideConfigurer();
526+
configurer.setLocation(new ClassPathResource("/org/mybatis/spring/annotation/override.properties"));
527+
return configurer;
528+
}
529+
530+
@Bean
531+
MyBean myBean() {
532+
return new MyBean("annotation");
533+
}
534+
}
535+
462536
@MapperScan(basePackages = "org.mybatis.spring.annotation.mapper.ds1", lazyInitialization = "${mybatis.lazy-initialization:false}")
463537
@PropertySource("classpath:/org/mybatis/spring/annotation/scan.properties")
464538
public static class LazyConfigWithPropertySource {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2010-2023 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.annotation;
17+
18+
public class MyBean {
19+
private String id;
20+
private String name;
21+
22+
public MyBean(String id) {
23+
this.id = id;
24+
}
25+
26+
public String getId() {
27+
return id;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright 2010-2023 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+
17+
myBean.name=MyBean!!
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright 2010-2023 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+
17+
scan-package=org.mybatis.spring.annotation.mapper.ds1
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2010-2023 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.config;
17+
18+
public class MyBean {
19+
private String id;
20+
private String name;
21+
22+
public MyBean(String id) {
23+
this.id = id;
24+
}
25+
26+
public String getId() {
27+
return id;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2010-2023 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.config;
17+
18+
import org.springframework.beans.factory.FactoryBean;
19+
20+
public class MyFactoryBean implements FactoryBean<MyBean> {
21+
@Override
22+
public MyBean getObject() {
23+
return new MyBean("factory");
24+
}
25+
26+
@Override
27+
public Class<?> getObjectType() {
28+
return MyBean.class;
29+
}
30+
}

src/test/java/org/mybatis/spring/config/NamespaceTest.java

Lines changed: 32 additions & 1 deletion
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-2023 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.
@@ -271,6 +271,37 @@ void testDefaultScope() {
271271
assertEquals(2, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());
272272
}
273273

274+
@Test
275+
void processPropertyPlaceHoldersIsTrue() {
276+
277+
applicationContext = new ClassPathXmlApplicationContext(
278+
new String[] { "org/mybatis/spring/config/process-property-placeholders-true.xml" }, setupSqlSessionTemplate());
279+
280+
startContext();
281+
282+
SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
283+
assertEquals(5, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());
284+
285+
MyBean myBean = applicationContext.getBean("myBean", MyBean.class);
286+
assertThat(myBean.getName()).isEqualTo("MyBean!!");
287+
}
288+
289+
@Test
290+
void processPropertyPlaceHoldersIsFalse() {
291+
292+
applicationContext = new ClassPathXmlApplicationContext(
293+
new String[] { "org/mybatis/spring/config/process-property-placeholders-false.xml" },
294+
setupSqlSessionTemplate());
295+
296+
startContext();
297+
298+
SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
299+
assertEquals(5, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());
300+
301+
MyBean myBean = applicationContext.getBean("myBean", MyBean.class);
302+
assertThat(myBean.getName()).isEqualTo("MyBean!!");
303+
}
304+
274305
private GenericApplicationContext setupSqlSessionTemplate() {
275306

276307
GenericApplicationContext genericApplicationContext = setupSqlSessionFactory();

0 commit comments

Comments
 (0)