作为一个想做架构师的程序猿,必须是一个优秀的程序猿。在引入某一个框架的时候,必需要研究源代码,将新的开源框架的风险变为可控性。
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 ));
}
孜孜不倦、厚积薄发。
----胡弦语录