Skip to content

Allow to specify custom MapperFactoryBean class on xml based bean definition #352

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
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
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2016 the original author or authors.
* Copyright 2010-2019 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 @@ -49,6 +49,7 @@ public class MapperScannerBeanDefinitionParser implements BeanDefinitionParser {
private static final String ATTRIBUTE_NAME_GENERATOR = "name-generator";
private static final String ATTRIBUTE_TEMPLATE_REF = "template-ref";
private static final String ATTRIBUTE_FACTORY_REF = "factory-ref";
private static final String ATTRIBUTE_MAPPER_FACTORY_BEAN_CLASS = "mapper-factory-bean-class";

/**
* {@inheritDoc}
Expand Down Expand Up @@ -77,6 +78,13 @@ public synchronized BeanDefinition parse(Element element, ParserContext parserCo
BeanNameGenerator nameGenerator = BeanUtils.instantiateClass(nameGeneratorClass, BeanNameGenerator.class);
scanner.setBeanNameGenerator(nameGenerator);
}
String mapperFactoryBeanClassName = element.getAttribute(ATTRIBUTE_MAPPER_FACTORY_BEAN_CLASS);
if (StringUtils.hasText(mapperFactoryBeanClassName)) {
@SuppressWarnings("unchecked")
Class<? extends MapperFactoryBean> mapperFactoryBeanClass =
(Class<? extends MapperFactoryBean>)classLoader.loadClass(mapperFactoryBeanClassName);
scanner.setMapperFactoryBeanClass(mapperFactoryBeanClass);
}
} catch (Exception ex) {
readerContext.error(ex.getMessage(), readerContext.extractSource(element), ex.getCause());
}
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/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-2018 the original author or authors.
Copyright 2010-2019 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 @@ -105,6 +105,21 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="mapper-factory-bean-class" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
The fully-qualified class name of the MapperFactoryBean to return a mybatis proxy as spring bean. (Since 2.0.1)
]]>
</xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:expected-type type="java.lang.Class" />
<tool:assignable-to type="org.mybatis.spring.mapper.MapperFactoryBean" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2018 the original author or authors.
* Copyright 2010-2019 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 @@ -108,6 +108,8 @@ public class MapperScannerConfigurer implements BeanDefinitionRegistryPostProces

private Class<?> markerInterface;

private Class<? extends MapperFactoryBean> mapperFactoryBeanClass;

private ApplicationContext applicationContext;

private String beanName;
Expand Down Expand Up @@ -241,6 +243,16 @@ public void setProcessPropertyPlaceHolders(boolean processPropertyPlaceHolders)
this.processPropertyPlaceHolders = processPropertyPlaceHolders;
}

/**
* The class of the {@link MapperFactoryBean} to return a mybatis proxy as spring bean.
*
* @param mapperFactoryBeanClass The class of the MapperFactoryBean
* @since 2.0.1
*/
public void setMapperFactoryBeanClass(Class<? extends MapperFactoryBean> mapperFactoryBeanClass) {
this.mapperFactoryBeanClass = mapperFactoryBeanClass;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -314,6 +326,7 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);
scanner.setResourceLoader(this.applicationContext);
scanner.setBeanNameGenerator(this.nameGenerator);
scanner.setMapperFactoryBeanClass(this.mapperFactoryBeanClass);
scanner.registerFilters();
scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ void testMarkerInterfaceAndAnnotationScan() {

@Test
void testCustomMapperFactoryBean() {
DummyMapperFactoryBean.clear();
applicationContext.register(AppConfigWithCustomMapperFactoryBean.class);

startContext();
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/mybatis/spring/config/NamespaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.mybatis.spring.config;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import org.junit.jupiter.api.AfterEach;
Expand All @@ -25,6 +26,7 @@
import org.mybatis.spring.mapper.MapperInterface;
import org.mybatis.spring.mapper.MapperSubinterface;
import org.mybatis.spring.mapper.child.MapperChildInterface;
import org.mybatis.spring.type.DummyMapperFactoryBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
Expand Down Expand Up @@ -170,6 +172,24 @@ void testScanWithExplicitSqlSessionTemplate() {
applicationContext.getBean("annotatedMapper");
}

@Test
void testScanWithMapperFactoryBeanClass() {
DummyMapperFactoryBean.clear();
applicationContext = new ClassPathXmlApplicationContext(
new String[] { "org/mybatis/spring/config/mapper-factory-bean-class.xml" }
, setupSqlSessionTemplate());

startContext();

// all interfaces with methods should be loaded
applicationContext.getBean("mapperInterface");
applicationContext.getBean("mapperSubinterface");
applicationContext.getBean("mapperChildInterface");
applicationContext.getBean("annotatedMapper");

assertTrue(DummyMapperFactoryBean.getMapperCount() > 0);
}

private GenericApplicationContext setupSqlSessionTemplate() {

GenericApplicationContext genericApplicationContext = setupSqlSessionFactory();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2010-2019 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

http://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.

-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

<mybatis:scan base-package="org.mybatis.spring.mapper"
mapper-factory-bean-class="org.mybatis.spring.type.DummyMapperFactoryBean"/>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.mybatis.spring.mapper;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.Properties;
Expand All @@ -28,6 +29,7 @@
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.child.MapperChildInterface;
import org.mybatis.spring.type.DummyMapperFactoryBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
Expand Down Expand Up @@ -254,6 +256,23 @@ void testScanWithPropertyPlaceholders() {
assertThat(sessionFactory.getConfiguration().getDefaultExecutorType()).isSameAs(ExecutorType.REUSE);
}

@Test
void testScanWithMapperFactoryBeanClass() {
DummyMapperFactoryBean.clear();
applicationContext.getBeanDefinition("mapperScanner").getPropertyValues().add(
"mapperFactoryBeanClass", DummyMapperFactoryBean.class);

startContext();

applicationContext.getBean("mapperInterface");
applicationContext.getBean("mapperSubinterface");
applicationContext.getBean("mapperChildInterface");
applicationContext.getBean("annotatedMapper");

assertTrue(DummyMapperFactoryBean.getMapperCount() > 0);
}


private void setupSqlSessionFactory(String name) {
GenericBeanDefinition definition = new GenericBeanDefinition();
definition.setBeanClass(SqlSessionFactoryBean.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2017 the original author or authors.
* Copyright 2010-2019 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 @@ -77,4 +77,9 @@ private SqlSessionFactory getCustomSessionFactoryForClass() {
public static int getMapperCount(){
return mapperInstanceCount.get();
}

public static void clear() {
mapperInstanceCount.set(0);
}

}