spring加载属性文件影响SqlSessionFactoryBean的bean和MapperScannerConfigurerbean扫描器自动注入问题

<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}这样的标签此时不生效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperLBY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值