<bean id="tomcatJdbcDataSourceFactory" class="com.qunar.db.resource.impl.TomcatJdbcDataSourceFactory"/>
<bean id="dataSource" class="com.qunar.db.resource.RWDelegatorDataSource">
<constructor-arg index="0" type="java.lang.String" value="${dictDataSource.jdbc.usernameSpace}"/>
<constructor-arg index="1" type="java.lang.String" value="${dictDataSource.jdbc.username}"/>
<constructor-arg index="2" type="java.lang.String" value="${dictDataSource.jdbc.password}"/>
<constructor-arg index="3" type="java.lang.String" value="${dictDataSource.jdbc.db_name}"/>
<constructor-arg index="4" type="int" value="20"/>
<constructor-arg index="5" type="int" value="50"/>
<constructor-arg index="6" type="java.lang.String" value="?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true"/>
<constructor-arg index="7" type="boolean" value="true"/>
<constructor-arg index="8" ref="tomcatJdbcDataSourceFactory"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath*:com.anly.portal.*.mapper/*Mapper.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.anly.portal.*.mapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
此时,启动会报异常,${jdbc.driver}这样的表达式获取不到properties里面的值,因为MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,会导致提前初始化一些类,这个时候,PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了,解决的办法如下:
修改为
<bean id="tomcatJdbcDataSourceFactory" class="com.qunar.db.resource.impl.TomcatJdbcDataSourceFactory"/>
<bean id="dataSource" class="com.qunar.db.resource.RWDelegatorDataSource">
<constructor-arg index="0" type="java.lang.String" value="${dictDataSource.jdbc.usernameSpace}"/>
<constructor-arg index="1" type="java.lang.String" value="${dictDataSource.jdbc.username}"/>
<constructor-arg index="2" type="java.lang.String" value="${dictDataSource.jdbc.password}"/>
<constructor-arg index="3" type="java.lang.String" value="${dictDataSource.jdbc.db_name}"/>
<constructor-arg index="4" type="int" value="20"/>
<constructor-arg index="5" type="int" value="50"/>
<constructor-arg index="6" type="java.lang.String" value="?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true"/>
<constructor-arg index="7" type="boolean" value="true"/>
<constructor-arg index="8" ref="tomcatJdbcDataSourceFactory"/>
</bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qunar.flight.interb2b.airport.dict.dao" />
<property name="sqlSessionFactoryBeanName" value="sessionFactory" />
</bean>
原理:使用sqlSessionFactoryBeanName注入,不会立即初始化sqlSessionFactory, 所以不会引发提前初始化问题,同时还应注意在配置org.mybatis.spring.SqlSessionFactoryBean
这个Bean时,id不能为sqlSessionFactory,如果为这样的话会导致MapperScannerConigurer在bean定义加载时,加载PropertyPlaceholderConfigurer还没来得及替换定义中的变量