Skip to content

Commit

Permalink
Support processPropertyPlaceHolders option in mapper:scan and @Mapper…
Browse files Browse the repository at this point in the history
…Scan

Fixes mybatisgh-829
  • Loading branch information
kazuki43zoo committed Jun 17, 2023
1 parent 9507fe0 commit 76e8bae
Show file tree
Hide file tree
Showing 15 changed files with 380 additions and 8 deletions.
13 changes: 12 additions & 1 deletion src/main/java/org/mybatis/spring/annotation/MapperScan.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 the original author or authors.
* Copyright 2010-2023 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.
Expand Down Expand Up @@ -185,4 +185,15 @@
*/
String defaultScope() default AbstractBeanDefinition.SCOPE_DEFAULT;

/**
* Specifies a flag that whether execute a property placeholder processing or not.
* <p>
* The default is {@literal true}. This means that a property placeholder processing execute.
*
* @since 3.0.3
*
* @return a flag that whether execute a property placeholder processing or not
*/
boolean processPropertyPlaceHolders() default true;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 the original author or authors.
* Copyright 2010-2023 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.
Expand Down Expand Up @@ -82,7 +82,7 @@ void registerBeanDefinitions(AnnotationMetadata annoMeta, AnnotationAttributes a
BeanDefinitionRegistry registry, String beanName) {

BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
builder.addPropertyValue("processPropertyPlaceHolders", true);
builder.addPropertyValue("processPropertyPlaceHolders", annoAttrs.getBoolean("processPropertyPlaceHolders"));

Class<? extends Annotation> annotationClass = annoAttrs.getClass("annotationClass");
if (!Annotation.class.equals(annotationClass)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 the original author or authors.
* Copyright 2010-2023 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.
Expand Down Expand Up @@ -56,6 +56,7 @@ public class MapperScannerBeanDefinitionParser extends AbstractBeanDefinitionPar
private static final String ATTRIBUTE_MAPPER_FACTORY_BEAN_CLASS = "mapper-factory-bean-class";
private static final String ATTRIBUTE_LAZY_INITIALIZATION = "lazy-initialization";
private static final String ATTRIBUTE_DEFAULT_SCOPE = "default-scope";
private static final String ATTRIBUTE_PROCESS_PROPERTY_PLACEHOLDERS = "process-property-placeholders";

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

ClassLoader classLoader = ClassUtils.getDefaultClassLoader();

builder.addPropertyValue("processPropertyPlaceHolders", true);
String processPropertyPlaceHolders = element.getAttribute(ATTRIBUTE_PROCESS_PROPERTY_PLACEHOLDERS);
builder.addPropertyValue("processPropertyPlaceHolders",
!StringUtils.hasText(processPropertyPlaceHolders) || Boolean.parseBoolean(processPropertyPlaceHolders));
try {
String annotationClassName = element.getAttribute(ATTRIBUTE_ANNOTATION);
if (StringUtils.hasText(annotationClassName)) {
Expand Down
12 changes: 11 additions & 1 deletion src/main/resources/org/mybatis/spring/config/mybatis-spring.xsd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2010-2022 the original author or authors.
Copyright 2010-2023 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.
Expand Down Expand Up @@ -148,6 +148,16 @@
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="process-property-placeholders" type="xsd:boolean">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
Specifies a flag that whether execute a property placeholder processing or not. (Since 3.0.3)
The default is true. This means that a property placeholder processing execute.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>
76 changes: 75 additions & 1 deletion src/test/java/org/mybatis/spring/annotation/MapperScanTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 the original author or authors.
* Copyright 2010-2023 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.
Expand Down Expand Up @@ -46,6 +46,7 @@
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
Expand Down Expand Up @@ -390,6 +391,32 @@ void testScopedProxyMapperScanByDefaultScope() {
assertEquals(2, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());
}

@Test
void testProcessPropertyPlaceHoldersIsTrue() {
applicationContext.register(ProcessPropertyPlaceHoldersTrueConfiguration.class);

startContext();

SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
assertEquals(1, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());

MyBean myBean = applicationContext.getBean(MyBean.class);
assertThat(myBean.getName()).isEqualTo("MyBean!!");
}

@Test
void testProcessPropertyPlaceHoldersIsFalse() {
applicationContext.register(ProcessPropertyPlaceHoldersFalseConfiguration.class);

startContext();

SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
assertEquals(1, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());

MyBean myBean = applicationContext.getBean(MyBean.class);
assertThat(myBean.getName()).isEqualTo("MyBean!!");
}

@Configuration
@MapperScan("org.mybatis.spring.mapper")
public static class AppConfigWithPackageScan {
Expand Down Expand Up @@ -459,6 +486,53 @@ static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer

}

@ComponentScan("org.mybatis.spring.annotation.factory")
@MapperScan(basePackages = "${scan-package}")
public static class ProcessPropertyPlaceHoldersTrueConfiguration {
@Bean
static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource("/org/mybatis/spring/annotation/placeholders.properties"));
return configurer;
}

@Bean
static PropertyOverrideConfigurer propertyOverrideConfigurer() {
PropertyOverrideConfigurer configurer = new PropertyOverrideConfigurer();
configurer.setLocation(new ClassPathResource("/org/mybatis/spring/annotation/override.properties"));
configurer.setIgnoreInvalidKeys(true);
return configurer;
}

@Bean
MyBean myBean() {
return new MyBean("annotation");
}
}

@ComponentScan("org.mybatis.spring.annotation.factory")
@MapperScan(basePackages = "org.mybatis.spring.annotation.mapper.ds1", processPropertyPlaceHolders = false)
public static class ProcessPropertyPlaceHoldersFalseConfiguration {
@Bean
static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource("/org/mybatis/spring/annotation/placeholders.properties"));
return configurer;
}

@Bean
static PropertyOverrideConfigurer propertyOverrideConfigurer() {
PropertyOverrideConfigurer configurer = new PropertyOverrideConfigurer();
configurer.setLocation(new ClassPathResource("/org/mybatis/spring/annotation/override.properties"));
return configurer;
}

@Bean
MyBean myBean() {
return new MyBean("annotation");
}
}

@MapperScan(basePackages = "org.mybatis.spring.annotation.mapper.ds1", lazyInitialization = "${mybatis.lazy-initialization:false}")
@PropertySource("classpath:/org/mybatis/spring/annotation/scan.properties")
public static class LazyConfigWithPropertySource {
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/mybatis/spring/annotation/MyBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2023 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.annotation;

public class MyBean {
private String id;
private String name;

public MyBean(String id) {
this.id = id;
}

public String getId() {
return id;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
17 changes: 17 additions & 0 deletions src/test/java/org/mybatis/spring/annotation/override.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2010-2023 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.
#

myBean.name=MyBean!!
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2010-2023 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.
#

scan-package=org.mybatis.spring.annotation.mapper.ds1
37 changes: 37 additions & 0 deletions src/test/java/org/mybatis/spring/config/MyBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2023 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.config;

public class MyBean {
private String id;
private String name;

public MyBean(String id) {
this.id = id;
}

public String getId() {
return id;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
30 changes: 30 additions & 0 deletions src/test/java/org/mybatis/spring/config/MyFactoryBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2023 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.config;

import org.springframework.beans.factory.FactoryBean;

public class MyFactoryBean implements FactoryBean<MyBean> {
@Override
public MyBean getObject() {
return new MyBean("factory");
}

@Override
public Class<?> getObjectType() {
return MyBean.class;
}
}
33 changes: 32 additions & 1 deletion src/test/java/org/mybatis/spring/config/NamespaceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 the original author or authors.
* Copyright 2010-2023 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.
Expand Down Expand Up @@ -271,6 +271,37 @@ void testDefaultScope() {
assertEquals(2, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());
}

@Test
void processPropertyPlaceHoldersIsTrue() {

applicationContext = new ClassPathXmlApplicationContext(
new String[] { "org/mybatis/spring/config/process-property-placeholders-true.xml" }, setupSqlSessionTemplate());

startContext();

SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
assertEquals(5, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());

MyBean myBean = applicationContext.getBean("myBean", MyBean.class);
assertThat(myBean.getName()).isEqualTo("MyBean!!");
}

@Test
void processPropertyPlaceHoldersIsFalse() {

applicationContext = new ClassPathXmlApplicationContext(
new String[] { "org/mybatis/spring/config/process-property-placeholders-false.xml" },
setupSqlSessionTemplate());

startContext();

SqlSessionFactory sqlSessionFactory = applicationContext.getBean(SqlSessionFactory.class);
assertEquals(5, sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers().size());

MyBean myBean = applicationContext.getBean("myBean", MyBean.class);
assertThat(myBean.getName()).isEqualTo("MyBean!!");
}

private GenericApplicationContext setupSqlSessionTemplate() {

GenericApplicationContext genericApplicationContext = setupSqlSessionFactory();
Expand Down
Loading

0 comments on commit 76e8bae

Please sign in to comment.