Skip to content

Custom FactoryBean on MapperScan annotation #40

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

Closed
Closed
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
10 changes: 8 additions & 2 deletions src/main/java/org/mybatis/spring/annotation/MapperScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
*
* @author Michael Lanyon
* @author Eduardo Macarron
*
*
* @since 1.2.0
* @see MapperScannerRegistrar
* @see MapperFactoryBean
Expand Down Expand Up @@ -101,7 +101,7 @@
* within the Spring container.
*/
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

/**
* This property specifies the annotation that the scanner will search for.
* <p>
Expand Down Expand Up @@ -136,4 +136,10 @@
*/
String sqlSessionFactoryRef() default "";

/**
* Specifies a custom MapperFactoryBean to return a mybatis proxy as spring bean.
*
*/
Class<? extends MapperFactoryBean> factoryBean() default MapperFactoryBean.class;

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
scanner.setBeanNameGenerator(BeanUtils.instantiateClass(generatorClass));
}

Class<? extends MapperFactoryBean> mapperFactoryBeanClass = annoAttrs.getClass("factoryBean");
if (!MapperFactoryBean.class.equals(mapperFactoryBeanClass)) {
scanner.setMapperFactoryBean(BeanUtils.instantiateClass(mapperFactoryBeanClass));
}

scanner.setSqlSessionTemplateBeanName(annoAttrs.getString("sqlSessionTemplateRef"));
scanner.setSqlSessionFactoryBeanName(annoAttrs.getString("sqlSessionFactoryRef"));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2010-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -70,6 +70,8 @@ public class ClassPathMapperScanner extends ClassPathBeanDefinitionScanner {

private Class<?> markerInterface;

private MapperFactoryBean mapperFactoryBean = new MapperFactoryBean();

public ClassPathMapperScanner(BeanDefinitionRegistry registry) {
super(registry, false);
}
Expand Down Expand Up @@ -102,6 +104,11 @@ public void setSqlSessionFactoryBeanName(String sqlSessionFactoryBeanName) {
this.sqlSessionFactoryBeanName = sqlSessionFactoryBeanName;
}

public void setMapperFactoryBean(MapperFactoryBean mapperFactoryBean) {
this.mapperFactoryBean = (mapperFactoryBean != null ? mapperFactoryBean : new MapperFactoryBean());
}


/**
* Configures parent scanner to search for the right interfaces. It can search
* for all interfaces or just for those that extends a markerInterface or/and
Expand Down Expand Up @@ -177,8 +184,8 @@ private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {

// the mapper interface is the original class of the bean
// but, the actual class of the bean is MapperFactoryBean
definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName());
definition.setBeanClass(MapperFactoryBean.class);
definition.getPropertyValues().add("mapperInterface", definition.getBeanClassName());
definition.setBeanClass(this.mapperFactoryBean.getClass());

definition.getPropertyValues().add("addToConfig", this.addToConfig);

Expand Down
75 changes: 46 additions & 29 deletions src/main/java/org/mybatis/spring/mapper/MapperFactoryBean.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2010-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -48,7 +48,7 @@
* Note that this factory can only inject <em>interfaces</em>, not concrete classes.
*
* @author Eduardo Macarron
*
*
* @see SqlSessionTemplate
* @version $Id$
*/
Expand All @@ -66,30 +66,6 @@ public MapperFactoryBean(Class<T> mapperInterface) {
public MapperFactoryBean() {
}

/**
* Sets the mapper interface of the MyBatis mapper
*
* @param mapperInterface class of the interface
*/
public void setMapperInterface(Class<T> mapperInterface) {
this.mapperInterface = mapperInterface;
}

/**
* If addToConfig is false the mapper will not be added to MyBatis. This means
* it must have been included in mybatis-config.xml.
* <p>
* If it is true, the mapper will be added to MyBatis in the case it is not already
* registered.
* <p>
* By default addToCofig is true.
*
* @param addToConfig
*/
public void setAddToConfig(boolean addToConfig) {
this.addToConfig = addToConfig;
}

/**
* {@inheritDoc}
*/
Expand All @@ -115,25 +91,66 @@ protected void checkDaoConfig() {
/**
* {@inheritDoc}
*/
@Override
public T getObject() throws Exception {
return getSqlSession().getMapper(this.mapperInterface);
}

/**
* {@inheritDoc}
*/
@Override
public Class<T> getObjectType() {
return this.mapperInterface;
}

/**
* {@inheritDoc}
*/
@Override
public boolean isSingleton() {
return true;
}

//------------- mutators --------------

/**
* Sets the mapper interface of the MyBatis mapper
*
* @param mapperInterface class of the interface
*/
public void setMapperInterface(Class<T> mapperInterface) {
this.mapperInterface = mapperInterface;
}

/**
* Return the mapper interface of the MyBatis mapper
*
* @return class of the interface
*/
public Class<T> getMapperInterface() {
return mapperInterface;
}

/**
* If addToConfig is false the mapper will not be added to MyBatis. This means
* it must have been included in mybatis-config.xml.
* <p/>
* If it is true, the mapper will be added to MyBatis in the case it is not already
* registered.
* <p/>
* By default addToCofig is true.
*
* @param addToConfig
*/
public void setAddToConfig(boolean addToConfig) {
this.addToConfig = addToConfig;
}

/**
* Return the flag for addition into MyBatis config.
*
* @return true if the mapper will be added to MyBatis in the case it is not already
* registered.
*/
public boolean isAddToConfig() {
return addToConfig;
}
}
27 changes: 24 additions & 3 deletions src/test/java/org/mybatis/spring/annotation/MapperScanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package org.mybatis.spring.annotation;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -27,6 +24,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 All @@ -39,6 +37,8 @@

import com.mockrunner.mock.jdbc.MockDataSource;

import static org.junit.Assert.*;

/**
* Test for the MapperScannerRegistrar.
* <p>
Expand Down Expand Up @@ -162,6 +162,22 @@ public void testMarkerInterfaceAndAnnotationScan() {
assertBeanNotLoaded("mapperInterface");
}

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

startContext();

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

assertTrue(DummyMapperFactoryBean.getMapperCount() > 0);

}

@Test
public void testScanWithNameConflict() {
GenericBeanDefinition definition = new GenericBeanDefinition();
Expand Down Expand Up @@ -265,6 +281,11 @@ public static class AppConfigWithSqlSessionFactory {
public static class AppConfigWithNameGenerator {
}

@Configuration
@MapperScan(basePackages = "org.mybatis.spring.mapper", factoryBean = DummyMapperFactoryBean.class)
public static class AppConfigWithCustomMapperFactoryBean {
}

public static class BeanNameGenerator implements org.springframework.beans.factory.support.BeanNameGenerator {

public String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry definitionRegistry) {
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/org/mybatis/spring/type/DummyMapperFactoryBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.mybatis.spring.type;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.mybatis.spring.mapper.MapperFactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.concurrent.atomic.AtomicInteger;

public class DummyMapperFactoryBean<T> extends MapperFactoryBean<T> {

private static final Logger LOGGER = Logger.getLogger(DummyMapperFactoryBean.class);

private static final AtomicInteger mapperInstanceCount = new AtomicInteger(0);

@Override
protected void checkDaoConfig() {
super.checkDaoConfig();
// make something more
if (isAddToConfig()) {
LOGGER.debug("register mapper for interface : " + getMapperInterface());

}
}

@Override
public T getObject() throws Exception {
MapperFactoryBean<T> mapperFactoryBean = new MapperFactoryBean<T>();
mapperFactoryBean.setMapperInterface(getMapperInterface());
mapperFactoryBean.setAddToConfig(isAddToConfig());
mapperFactoryBean.setSqlSessionFactory(getCustomSessionFactoryForClass(getMapperInterface()));
T object = mapperFactoryBean.getObject();
mapperInstanceCount.incrementAndGet();
return object;
}



private SqlSessionFactory getCustomSessionFactoryForClass(Class mapperClass) {
// can for example read a custom annotation to set a custom sqlSessionFactory

// just a dummy implementation example
return (SqlSessionFactory) Proxy.newProxyInstance(
SqlSessionFactory.class.getClassLoader(),
new Class[]{SqlSessionFactory.class},
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("getConfiguration".equals(method.getName())) {
return getSqlSession().getConfiguration();
}
// dummy
return null;
}
});
}

public static final int getMapperCount(){
return mapperInstanceCount.get();
}
}