- All Implemented Interfaces:
org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanNameAware,org.springframework.beans.factory.config.BeanFactoryPostProcessor,org.springframework.beans.factory.InitializingBean,org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor,org.springframework.context.ApplicationContextAware
public class MapperScannerConfigurer
extends java.lang.Object
implements org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.BeanNameAware
MapperFactoryBean. Note that only interfaces with at least one method will be registered;
concrete classes will be ignored.
This class was a {code BeanFactoryPostProcessor} until 1.0.1 version. It changed to
BeanDefinitionRegistryPostProcessor in 1.0.2. See https://jira.springsource.org/browse/SPR-8269 for the
details.
The basePackage property can contain more than one package name, separated by either commas or semicolons.
This class supports filtering the mappers created by either specifying a marker interface or an annotation. The
annotationClass property specifies an annotation to search for. The markerInterface property
specifies a parent interface to search for. If both properties are specified, mappers are added for interfaces that
match either criteria. By default, these two properties are null, so all interfaces in the given
basePackage are added as mappers.
This configurer enables autowire for all the beans that it creates so that they are automatically autowired with the
proper SqlSessionFactory or SqlSessionTemplate. If there is more than one SqlSessionFactory
in the application, however, autowiring cannot be used. In this case you must explicitly specify either an
SqlSessionFactory or an SqlSessionTemplate to use via the bean name properties. Bean names
are used rather than actual objects because Spring does not initialize property placeholders until after this class
is processed.
Passing in an actual object which may require placeholders (i.e. DB user password) will fail. Using bean names defers
actual object creation until later in the startup process, after all placeholder substitution is completed. However,
note that this configurer does support property placeholders of its own properties. The
basePackage and bean name properties all support ${property} style substitution.
Configuration sample:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mybatis.spring.sample.mapper" />
<!-- optional unless there are multiple session factories defined -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
- Author:
- Hunter Presnall, Eduardo Macarron
- See Also:
MapperFactoryBean,ClassPathMapperScanner
-
Constructor Summary
Constructors Constructor Description MapperScannerConfigurer() -
Method Summary
Modifier and Type Method Description voidafterPropertiesSet()org.springframework.beans.factory.support.BeanNameGeneratorgetNameGenerator()Gets beanNameGenerator to be used while running the scanner.voidpostProcessBeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)voidpostProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)voidsetAddToConfig(boolean addToConfig)Same asMapperFactoryBean#setAddToConfig(boolean).voidsetAnnotationClass(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)This property specifies the annotation that the scanner will search for.voidsetApplicationContext(org.springframework.context.ApplicationContext applicationContext)voidsetBasePackage(java.lang.String basePackage)This property lets you set the base package for your mapper interface files.voidsetBeanName(java.lang.String name)voidsetDefaultScope(java.lang.String defaultScope)Sets the default scope of scanned mappers.voidsetLazyInitialization(java.lang.String lazyInitialization)Set whether enable lazy initialization for mapper bean.voidsetMapperFactoryBeanClass(java.lang.Class<? extends MapperFactoryBean> mapperFactoryBeanClass)The class of theMapperFactoryBeanto return a mybatis proxy as spring bean.voidsetMarkerInterface(java.lang.Class<?> superClass)This property specifies the parent that the scanner will search for.voidsetNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator nameGenerator)Sets beanNameGenerator to be used while running the scanner.voidsetProcessPropertyPlaceHolders(boolean processPropertyPlaceHolders)Specifies a flag that whether execute a property placeholder processing or not.voidsetSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)Deprecated.voidsetSqlSessionFactoryBeanName(java.lang.String sqlSessionFactoryName)Specifies whichSqlSessionFactoryto use in the case that there is more than one in the spring context.voidsetSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate)Deprecated.UsesetSqlSessionTemplateBeanName(String)insteadvoidsetSqlSessionTemplateBeanName(java.lang.String sqlSessionTemplateName)Specifies whichSqlSessionTemplateto use in the case that there is more than one in the spring context.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
MapperScannerConfigurer
public MapperScannerConfigurer()
-
-
Method Details
-
setBasePackage
public void setBasePackage(java.lang.String basePackage)This property lets you set the base package for your mapper interface files.You can set more than one package by using a semicolon or comma as a separator.
Mappers will be searched for recursively starting in the specified package(s).
- Parameters:
basePackage- base package name
-
setAddToConfig
public void setAddToConfig(boolean addToConfig)Same asMapperFactoryBean#setAddToConfig(boolean).- Parameters:
addToConfig- a flag that whether add mapper to MyBatis or not- See Also:
MapperFactoryBean.setAddToConfig(boolean)
-
setLazyInitialization
public void setLazyInitialization(java.lang.String lazyInitialization)Set whether enable lazy initialization for mapper bean.Default is
false.- Parameters:
lazyInitialization- Set the @{code true} to enable- Since:
- 2.0.2
-
setAnnotationClass
public void setAnnotationClass(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)This property specifies the annotation that the scanner will search for.The scanner will register all interfaces in the base package that also have the specified annotation.
Note this can be combined with markerInterface.
- Parameters:
annotationClass- annotation class
-
setMarkerInterface
public void setMarkerInterface(java.lang.Class<?> superClass)This property specifies the parent that the scanner will search for.The scanner will register all interfaces in the base package that also have the specified interface class as a parent.
Note this can be combined with annotationClass.
- Parameters:
superClass- parent class
-
setSqlSessionTemplate
Deprecated.UsesetSqlSessionTemplateBeanName(String)insteadSpecifies whichSqlSessionTemplateto use in the case that there is more than one in the spring context. Usually this is only needed when you have more than one datasource.- Parameters:
sqlSessionTemplate- a template of SqlSession
-
setSqlSessionTemplateBeanName
public void setSqlSessionTemplateBeanName(java.lang.String sqlSessionTemplateName)Specifies whichSqlSessionTemplateto use in the case that there is more than one in the spring context. Usually this is only needed when you have more than one datasource.Note bean names are used, not bean references. This is because the scanner loads early during the start process and it is too early to build mybatis object instances.
- Parameters:
sqlSessionTemplateName- Bean name of theSqlSessionTemplate- Since:
- 1.1.0
-
setSqlSessionFactory
@Deprecated public void setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)Deprecated.UsesetSqlSessionFactoryBeanName(String)instead.Specifies whichSqlSessionFactoryto use in the case that there is more than one in the spring context. Usually this is only needed when you have more than one datasource.- Parameters:
sqlSessionFactory- a factory of SqlSession
-
setSqlSessionFactoryBeanName
public void setSqlSessionFactoryBeanName(java.lang.String sqlSessionFactoryName)Specifies whichSqlSessionFactoryto use in the case that there is more than one in the spring context. Usually this is only needed when you have more than one datasource.Note bean names are used, not bean references. This is because the scanner loads early during the start process and it is too early to build mybatis object instances.
- Parameters:
sqlSessionFactoryName- Bean name of theSqlSessionFactory- Since:
- 1.1.0
-
setProcessPropertyPlaceHolders
public void setProcessPropertyPlaceHolders(boolean processPropertyPlaceHolders)Specifies a flag that whether execute a property placeholder processing or not.The default is false. This means that a property placeholder processing does not execute.
- Parameters:
processPropertyPlaceHolders- a flag that whether execute a property placeholder processing or not- Since:
- 1.1.1
-
setMapperFactoryBeanClass
public void setMapperFactoryBeanClass(java.lang.Class<? extends MapperFactoryBean> mapperFactoryBeanClass)The class of theMapperFactoryBeanto return a mybatis proxy as spring bean.- Parameters:
mapperFactoryBeanClass- The class of the MapperFactoryBean- Since:
- 2.0.1
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)- Specified by:
setApplicationContextin interfaceorg.springframework.context.ApplicationContextAware
-
setBeanName
public void setBeanName(java.lang.String name)- Specified by:
setBeanNamein interfaceorg.springframework.beans.factory.BeanNameAware
-
getNameGenerator
public org.springframework.beans.factory.support.BeanNameGenerator getNameGenerator()Gets beanNameGenerator to be used while running the scanner.- Returns:
- the beanNameGenerator BeanNameGenerator that has been configured
- Since:
- 1.2.0
-
setNameGenerator
public void setNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator nameGenerator)Sets beanNameGenerator to be used while running the scanner.- Parameters:
nameGenerator- the beanNameGenerator to set- Since:
- 1.2.0
-
setDefaultScope
public void setDefaultScope(java.lang.String defaultScope)Sets the default scope of scanned mappers.Default is
null(equiv to singleton).- Parameters:
defaultScope- the default scope- Since:
- 2.0.6
-
afterPropertiesSet
public void afterPropertiesSet() throws java.lang.Exception- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
java.lang.Exception
-
postProcessBeanFactory
public void postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)- Specified by:
postProcessBeanFactoryin interfaceorg.springframework.beans.factory.config.BeanFactoryPostProcessor
-
postProcessBeanDefinitionRegistry
public void postProcessBeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)- Specified by:
postProcessBeanDefinitionRegistryin interfaceorg.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor- Since:
- 1.0.2
-
setSqlSessionFactoryBeanName(String)instead.