<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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"
//default-autowire="byName":根据名称进行自动注入功能
default-autowire="byName">
一、不使用.properties属性配置文件
不使用.properties属性配置文件时MapperScannerConfigurer的name和SqlSessionFactoryBean的bean标签的id相同可以实现自动注入,MapperScannerConfigurer的第二个property可以不用配置
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
//下面标签可以自动注入,不需要配置
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.lby.pojo"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lby.mapper"></property>
//ref的值和SqlSessionFactoryBean的bean标签的ID相同,可以实现自动注入,可以配置下面的标签
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
二、使用.properties属性配置文件
1、当加入<context:property-placeholder>标签后,MapperScannerConfigurer不能使用自动注入的方式
2、SqlSessionFactoryBean的bean的id名不能为sqlSessionFactory,也就是不可以和MapperScannerConfigurer的bean标签的property属性的name相同
3、MapperScannerConfigurer的bean标签的下面property的name不能用SqlSessionFactory
4、只能使用sqlSessionFactoryBeanName才会配置生效,且不能和SqlSessionFactoryBean的bean标签的id相同
<context:property-placeholder location="classpath:lby01.properties,classpath:lby02.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
//下面标签可以自动注入,不需要配置
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.lby.pojo"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lby.mapper"></property>
//这个标签必须配置,不能实现自动注入
<property name="sqlSessionFactoryBeanName" value="factory"></property>
</bean>
三、产生问题的原因(以下观点为猜测,目前我没能力证实,请谨慎观看)
使用自动注入时,会优先实例化的自动注入的类,当DriverManagerDataSource注册完成之后,.properties属性文件还没有加载,所以${jdbc.username}这样的标签此时不生效。