作为一个想做架构师的程序员,必须是一个优秀的程序员,在引入某一个框架的时候,必须要研究源码,将新的开源框架的风险变为可控性。
1.Spring结合Mybatis最常用的配置。
<!–理论加实践,才是架构师嘚最佳实践 –>
<!–JDBC Data Source –>
<
bean
id
=
“testdataSource”
class
=
“org.springframework.jdbc.datasource.DriverManagerDataSource”
>
<
property
name
=
“driverClassName”
value
=
“oracle.jdbc.driver.OracleDriver”
/>
<!– <property name=”url” value=”jdbc:oracle:thin:@127.0.0.1:1521:orcl“
/> –>
<
property
name
=
“url”
value
=
“jdbc:oracle:thin:@192.168.137.101:1521:ora92”
/>
<
property
name
=
“username”
value
=
“sale”
/>
<
property
name
=
“password”
value
=
“sale”
/>
</
bean
>
<
bean
id
=
“sqlSessionFactory”
class
=
“org.mybatis.spring.SqlSessionFactoryBean”
>
<
property
name
=
“dataSource”
ref
=
“testdataSource”
/>
<
property
name
=
“mapperLocations”
>
<
list
>
<
value
>
classpath:com/etrading /mapping/**/*Mapper.xml
</
value
>
</
list
>
</
property
>
<!– <property name=”typeAliasesPackage” value=”com.nicchagil.mybatis3spring3intg.bean”
/> –>
</
bean
>
<
bean
class
=
“org.mybatis.spring.mapper.MapperScannerConfigurer”
>
<
property
name
=
“basePackage”
value
=
“com/etrading/mapping/common;com/etrading/mapping/account”
/>
<
property
name
=
“sqlSessionFactoryBeanName”
value
=
“sqlSessionFactory”
/>
</
bean
>
1)Spring为了整合Mybatis,专门封装了一个jar包(mybatis-spring-1.2.3.jar),其中
MapperScannerConfigurer封装了自动扫描全局路径下的Mapper
2)MapperScannerConfigurer源码分析(基于mybatis-spring-1.2.3.jar)
其实要弄通源码,一定要弄通spring容器源码,这个是基础(理解框架设计的思想,其实很多代码思想是可以复用的)
简单的梳理下Spring容器思想:
Spring容器环境起来–>通过refresh()方法刷新容器(比较简洁)
MapperScannerConfigurer就是在刷新容器的过程中自动的执行方法
postProcessBeanDefinitionRegistry
/**
*
{@inheritDoc}
* 通过Spring容器触发
postProcessBeanDefinitionRegistry
*
@since
1.0.2
*/
@Override
public
void
postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
if
(
this
.
processPropertyPlaceHolders
) {
processPropertyPlaceHolders();
}
//核心处理类
ClassPathMapperScanner,通过反射机制加载注解bean到spring容器
ClassPathMapperScanner scanner =
new
ClassPathMapperScanner(registry);
scanner.setAddToConfig(
this
.
addToConfig
);
scanner.setAnnotationClass(
this
.
annotationClass
);
scanner.setMarkerInterface(
this
.
markerInterface
);
scanner.setSqlSessionFactory(
this
.
sqlSessionFactory
);//不需要在xml中设置
sqlSessionFactory以及
sqlSessionTemplate
scanner.setSqlSessionTemplate(
this
.
sqlSessionTemplate
);
scanner.setSqlSessionFactoryBeanName(
this
.
sqlSessionFactoryBeanName
);
scanner.setSqlSessionTemplateBeanName(
this
.
sqlSessionTemplateBeanName
);
scanner.setResourceLoader(
this
.
applicationContext
);
scanner.setBeanNameGenerator(
this
.
nameGenerator
);
scanner.registerFilters();
//真正自动化扫描的类
scanner.scan(StringUtils. tokenizeToStringArray(
this
.
basePackage
, ConfigurableApplicationContext.
CONFIG_LOCATION_DELIMITERS
));
} 孜孜不倦、厚积薄发。 —-胡弦语录
Mybatis结合Spring注解自动扫描源码分析
原文作者:Spring Cloud
原文地址: https://blog.csdn.net/huxian1234/article/details/47247723
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/huxian1234/article/details/47247723
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。