序
纵有千古,横有八荒.前途似海,来日方长!!! spring源码的学习是所有java开发者进阶而必须要去做的.所以,准备花费两个月去学习spring的源码,记录下自己的收获到博客中,加深印象.也方便以后忘记后的复习
文件配置工作
回到spring最初的时候,印象里是那冗长的xml文件的配置,然而学成以后,在正式的上班中,xml文件已经离我远去,spring的xml文件配置已经被更加便捷的注解所取代.不过,在我们开始准备阅读spring源码之时,基于xml文件去理解spring的工作过程,这是一个较为合理的学习方式.首先看下xml文件的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--扫描service包-->
<context:component-scan base-package="xxxxx"></context:component-scan>
<!--创建dbcp数据源连接池-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!--创建mybatis的SqlSessionFactory工厂类对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!---->
<!--<property name="configLocation" value="classpath:mybatis.xml"></property>-->
<!--指定mapper文件的地址 此处可以使用*号同配置,表示加载包下所有的xml结尾的mapper文件-->
<property name="mapperLocations" value="classpath:/mapper/*.xml"></property>
</bean>
<!--配置扫描mybatis的dao接口 ,会为dao接口创建myabtis的dao接口实现类对象,放置到session工厂中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="ssm.shiro.dao"></property>
</bean>
<!--声明spring 的事物管理器对象-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--声明以注解的方式配置spring 的事物-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--引入spring 和shiro集成的主配置文件-->
<import resource="classpath:spring-shiro.xml"></import>
</beans>
网上找的一份xml文件的配置,当时只是需要这么复制粘贴一份就可以玩spring了,然而在这份xml文件背后的故事又是怎么样的呢?
初始化的方法
xml文件的初始话,最后都会调用到AbstractApplicationContext这个类的refresh方法中,后续的章节都是以此作为主体,去分析学习spring的源码
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
prepareRefresh();
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
prepareBeanFactory(beanFactory);
try {
postProcessBeanFactory(beanFactory);
invokeBeanFactoryPostProcessors(beanFactory);
registerBeanPostProcessors(beanFactory);
initMessageSource();
initApplicationEventMulticaster();
onRefresh();
registerListeners();
finishBeanFactoryInitialization(beanFactory);
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
destroyBeans();
cancelRefresh(ex);
throw ex;
}
finally {
resetCommonCaches();
}
}
}
结
此篇仅仅是一些配置,笔者所述的spring源码为5.2.8版本