Skip to content

Commit c3351a3

Browse files
committed
Prevent unnecessary generating instance of MapperFactoryBean
1 parent b064232 commit c3351a3

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void registerBeanDefinitions(AnnotationAttributes annoAttrs, BeanDefinitionRegis
9797

9898
Class<? extends MapperFactoryBean> mapperFactoryBeanClass = annoAttrs.getClass("factoryBean");
9999
if (!MapperFactoryBean.class.equals(mapperFactoryBeanClass)) {
100-
scanner.setMapperFactoryBean(BeanUtils.instantiateClass(mapperFactoryBeanClass));
100+
scanner.setMapperFactoryBeanClass(mapperFactoryBeanClass);
101101
}
102102

103103
scanner.setSqlSessionTemplateBeanName(annoAttrs.getString("sqlSessionTemplateRef"));

src/main/java/org/mybatis/spring/mapper/ClassPathMapperScanner.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2017 the original author or authors.
2+
* Copyright 2010-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,7 +69,7 @@ public class ClassPathMapperScanner extends ClassPathBeanDefinitionScanner {
6969

7070
private Class<?> markerInterface;
7171

72-
private MapperFactoryBean<?> mapperFactoryBean = new MapperFactoryBean<>();
72+
private Class<? extends MapperFactoryBean> mapperFactoryBeanClass = MapperFactoryBean.class;
7373

7474
public ClassPathMapperScanner(BeanDefinitionRegistry registry) {
7575
super(registry, false);
@@ -103,10 +103,23 @@ public void setSqlSessionFactoryBeanName(String sqlSessionFactoryBeanName) {
103103
this.sqlSessionFactoryBeanName = sqlSessionFactoryBeanName;
104104
}
105105

106+
/**
107+
* @deprecated Since 2.0.1, Please use the {@link #setMapperFactoryBeanClass(Class)}.
108+
*/
109+
@Deprecated
106110
public void setMapperFactoryBean(MapperFactoryBean<?> mapperFactoryBean) {
107-
this.mapperFactoryBean = mapperFactoryBean != null ? mapperFactoryBean : new MapperFactoryBean<>();
111+
this.mapperFactoryBeanClass = mapperFactoryBean == null ? MapperFactoryBean.class : mapperFactoryBean.getClass();
108112
}
109113

114+
/**
115+
* Set the {@code MapperFactoryBean} class.
116+
*
117+
* @param mapperFactoryBeanClass the {@code MapperFactoryBean} class
118+
* @since 2.0.1
119+
*/
120+
public void setMapperFactoryBeanClass(Class<? extends MapperFactoryBean> mapperFactoryBeanClass) {
121+
this.mapperFactoryBeanClass = mapperFactoryBeanClass == null ? MapperFactoryBean.class : mapperFactoryBeanClass;
122+
}
110123

111124
/**
112125
* Configures parent scanner to search for the right interfaces. It can search
@@ -174,7 +187,7 @@ private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
174187
// the mapper interface is the original class of the bean
175188
// but, the actual class of the bean is MapperFactoryBean
176189
definition.getConstructorArgumentValues().addGenericArgumentValue(beanClassName); // issue #59
177-
definition.setBeanClass(this.mapperFactoryBean.getClass());
190+
definition.setBeanClass(this.mapperFactoryBeanClass);
178191

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

0 commit comments

Comments
 (0)