Skip to content

Support 'mybatis.mapper-default-scope' as configuration property #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,16 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
builder.addPropertyValue("annotationClass", Mapper.class);
builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(packages));
BeanWrapper beanWrapper = new BeanWrapperImpl(MapperScannerConfigurer.class);
Stream.of(beanWrapper.getPropertyDescriptors())
// Need to mybatis-spring 2.0.2+
.filter(x -> x.getName().equals("lazyInitialization")).findAny()
.ifPresent(x -> builder.addPropertyValue("lazyInitialization", "${mybatis.lazy-initialization:false}"));
Set<String> propertyNames = Stream.of(beanWrapper.getPropertyDescriptors()).map(PropertyDescriptor::getName)
.collect(Collectors.toSet());
if (propertyNames.contains("lazyInitialization")) {
// Need to mybatis-spring 2.0.2+
builder.addPropertyValue("lazyInitialization", "${mybatis.lazy-initialization:false}");
}
if (propertyNames.contains("defaultScope")) {
// Need to mybatis-spring 2.0.6+
builder.addPropertyValue("defaultScope", "${mybatis.mapper-default-scope:}");
}
registry.registerBeanDefinition(MapperScannerConfigurer.class.getName(), builder.getBeanDefinition());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"description": "Set whether enable lazy initialization for mapper bean.",
"type": "java.lang.Boolean"
},
{
"defaultValue": "",
"name": "mybatis.mapper-default-scope",
"description": "A default scope for mapper bean that scanned by auto-configure.",
"type": "java.lang.String"
},
{
"name": "mybatis.scripting-language-driver.velocity.userdirective",
"deprecation": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Available properties are:
| `default-scripting-language-driver` | The default scripting language driver class. This feature requires to use together with mybatis-spring 2.0.2+. |
| `configuration-properties` | Externalized properties for MyBatis configuration. Specified properties can be used as placeholder on MyBatis config file and Mapper file. For detail see the [MyBatis reference page](http://www.mybatis.org/mybatis-3/configuration.html#properties). |
| `lazy-initialization` | Whether enable lazy initialization of mapper bean. Set `true` to enable lazy initialization. This feature requires to use together with mybatis-spring 2.0.2+. |
| `mapper-default-scope` | Default scope for mapper bean that scanned by auto-configure. This feature requires to use together with mybatis-spring 2.0.6+. |
| `configuration.*` | Property keys for `Configuration` bean provided by MyBatis Core. About available nested properties see the [MyBatis reference page](http://www.mybatis.org/mybatis-3/configuration.html#settings). <span class="label important">NOTE</span>: This property cannot be used at the same time with the `config-location`. |
| `scripting-language-driver.thymeleaf.*` | Property keys for `ThymeleafLanguageDriverConfig` bean provided by MyBatis Thymeleaf. About available nested properties see the [MyBatis Thymeleaf reference page](http://www.mybatis.org/thymeleaf-scripting/user-guide.html#_configuration_properties). |
| `scripting-language-driver.freemarker.*` | Properties keys for `FreeMarkerLanguageDriverConfig` bean provided by MyBatis FreeMarker. About available nested properties see the [MyBatis FreeMarker reference page](http://www.mybatis.org/freemarker-scripting/#Configuration). This feature requires to use together with mybatis-freemarker 1.2.0+. |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2015-2019 the original author or authors.
* Copyright 2015-2020 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 @@ -44,7 +44,7 @@ void testProperties() throws IOException {

List<Map<String, Object>> properties = documentContext.read("$.properties");

assertThat(properties.size()).isEqualTo(4);
assertThat(properties.size()).isEqualTo(5);

// assert for mybatis.configuration.default-scripting-language
{
Expand Down Expand Up @@ -78,9 +78,17 @@ void testProperties() throws IOException {
assertThat(element.get("type")).isEqualTo("java.lang.Boolean");
}

// assert for mybatis.scripting-language-driver.velocity.userdirective
// assert for mybatis.mapper-default-scope
{
Map<String, Object> element = properties.get(3);
assertThat(element.get("defaultValue")).isEqualTo("");
assertThat(element.get("name")).isEqualTo("mybatis.mapper-default-scope");
assertThat(element.get("type")).isEqualTo("java.lang.String");
}

// assert for mybatis.scripting-language-driver.velocity.userdirective
{
Map<String, Object> element = properties.get(4);
assertThat(element.get("name")).isEqualTo("mybatis.scripting-language-driver.velocity.userdirective");
@SuppressWarnings("unchecked")
Map<String, Object> deprecation = (Map<String, Object>) element.get("deprecation");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.aop.scope.ScopedProxyFactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
Expand All @@ -78,6 +83,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.SimpleThreadScope;

/**
* Tests for {@link MybatisAutoConfiguration}
Expand Down Expand Up @@ -193,6 +199,35 @@ void testAutoScanWithLazy() {
assertThat(sqlSessionFactory.getConfiguration().getMapperRegistry().getMappers()).hasSize(1);
}

@Test
void testAutoScanWithDefaultScope() {
TestPropertyValues.of("mybatis.mapper-default-scope:thread").applyTo(this.context);
this.context.register(EmbeddedDataSourceConfiguration.class, MybatisBootMapperScanAutoConfiguration.class,
MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
{
this.context.getBean(CityMapper.class);
BeanDefinition bd = this.context.getBeanDefinition("cityMapper");
assertThat(bd.getBeanClassName()).isEqualTo(ScopedProxyFactoryBean.class.getName());
BeanDefinition spbd = this.context.getBeanDefinition("scopedTarget.cityMapper");
assertThat(spbd.getBeanClassName()).isEqualTo(MapperFactoryBean.class.getName());
assertThat(spbd.getScope()).isEqualTo("thread");
}
}

@Test
void testAutoScanWithoutDefaultScope() {
this.context.register(EmbeddedDataSourceConfiguration.class, MybatisBootMapperScanAutoConfiguration.class,
MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
{
this.context.getBean(CityMapper.class);
BeanDefinition df = this.context.getBeanDefinition("cityMapper");
assertThat(df.getBeanClassName()).isEqualTo(MapperFactoryBean.class.getName());
assertThat(df.getScope()).isEqualTo("singleton");
}
}

@Test
void testWithConfigLocation() {
TestPropertyValues.of("mybatis.config-location:mybatis-config.xml").applyTo(this.context);
Expand Down Expand Up @@ -745,7 +780,11 @@ static MapperScannerConfigurer mapperScannerConfigurer() {

@Configuration
@EnableAutoConfiguration
static class MybatisBootMapperScanAutoConfiguration {
static class MybatisBootMapperScanAutoConfiguration implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerScope("thread", new SimpleThreadScope());
}
}

@Configuration
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

<properties>
<mybatis.version>3.5.5</mybatis.version>
<mybatis-spring.version>2.0.5</mybatis-spring.version>
<mybatis-spring.version>2.0.6-SNAPSHOT</mybatis-spring.version>
<mybatis-freemarker.version>1.2.2</mybatis-freemarker.version>
<mybatis-velocity.version>2.1.0</mybatis-velocity.version>
<mybatis-thymeleaf.version>1.0.2</mybatis-thymeleaf.version>
Expand Down