diff --git a/src/main/java/org/mybatis/spring/mapper/MapperFactoryBean.java b/src/main/java/org/mybatis/spring/mapper/MapperFactoryBean.java
index 320cdb6035..3d9dd04985 100644
--- a/src/main/java/org/mybatis/spring/mapper/MapperFactoryBean.java
+++ b/src/main/java/org/mybatis/spring/mapper/MapperFactoryBean.java
@@ -48,14 +48,14 @@
* Note that this factory can only inject interfaces, not concrete classes.
*
* @author Eduardo Macarron
- *
+ *
* @see SqlSessionTemplate
* @version $Id$
*/
public class MapperFactoryBean extends SqlSessionDaoSupport implements FactoryBean {
+ private T mapperInstance;
private Class mapperInterface;
-
private boolean addToConfig = true;
/**
@@ -108,14 +108,19 @@ protected void checkDaoConfig() {
* {@inheritDoc}
*/
public T getObject() throws Exception {
- return getSqlSession().getMapper(this.mapperInterface);
+ if (this.mapperInstance == null) {
+ this.mapperInstance = getSqlSession().getMapper(this.mapperInterface);
+ }
+ return this.mapperInstance;
}
/**
* {@inheritDoc}
*/
- public Class getObjectType() {
- return this.mapperInterface;
+ public Class extends T> getObjectType() {
+ return this.mapperInstance != null
+ ? this.mapperInstance.getClass().asSubclass(this.mapperInterface)
+ : this.mapperInterface;
}
/**
diff --git a/src/test/java/org/mybatis/spring/submitted/mappertype/IFooMapper.java b/src/test/java/org/mybatis/spring/submitted/mappertype/IFooMapper.java
new file mode 100644
index 0000000000..b153e506c3
--- /dev/null
+++ b/src/test/java/org/mybatis/spring/submitted/mappertype/IFooMapper.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2013 MyBatis.org.
+ *
+ * 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.
+ */
+package org.mybatis.spring.submitted.mappertype;
+
+import org.apache.ibatis.annotations.Select;
+
+public interface IFooMapper {
+
+ @Select("select * from foo")
+ int selectInt();
+}
diff --git a/src/test/java/org/mybatis/spring/submitted/mappertype/MapperTypeTest.java b/src/test/java/org/mybatis/spring/submitted/mappertype/MapperTypeTest.java
new file mode 100644
index 0000000000..89ada59b08
--- /dev/null
+++ b/src/test/java/org/mybatis/spring/submitted/mappertype/MapperTypeTest.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2013 MyBatis.org.
+ *
+ * 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.
+ */
+package org.mybatis.spring.submitted.mappertype;
+
+import java.lang.reflect.Proxy;
+import java.util.Collection;
+import static org.hamcrest.CoreMatchers.*;
+import org.hamcrest.Matcher;
+import static org.junit.Assert.*;
+import org.junit.Test;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class MapperTypeTest {
+
+ @Test
+ public void shouldContextReturnMappersActualType() {
+ final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:org/mybatis/spring/submitted/mappertype/applicationContext.xml");
+ try {
+ final IFooMapper mapper = ctx.getBean(IFooMapper.class);
+ assertNotNull("mapper must not be null", mapper);
+ assertThat("mapper should be a proxy", mapper, instanceOf(Proxy.class));
+
+ final Collection proxies = ctx.getBeansOfType(Proxy.class).values();
+ final Matcher mapperMatcher = instanceOf(IFooMapper.class);
+ assertThat("collection of all proxies should contain mapper", proxies, hasItem(mapperMatcher));
+ } finally {
+ ctx.close();
+ }
+ }
+
+}
diff --git a/src/test/java/org/mybatis/spring/submitted/mappertype/applicationContext.xml b/src/test/java/org/mybatis/spring/submitted/mappertype/applicationContext.xml
new file mode 100644
index 0000000000..20827c4a74
--- /dev/null
+++ b/src/test/java/org/mybatis/spring/submitted/mappertype/applicationContext.xml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+