在配置多数据源时候在service层的时候如果只用@Autowired的时候会因为有多个数据源而报错,但当我加上@Qualifier("sqlSessionFactory")的时候又会查不到对应的bean是我的注解写的有问题?
配置文件如下
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 读取数据库连接信息配置 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:db.properties" />
</bean>
<!-- 配置数据源连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="100"/>
<property name="initialSize" value="5"/>
<property name="minIdle" value="5"/>
<property name="maxWait" value="60"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="false"/>
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
</bean>
<!-- 配置数据源管理对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> -->
<property name="mapperLocations" value="classpath:com/ocss/sqlmap/*.xml" />
<property name="typeAliasesPackage" value="com.ocss.dao"/>
</bean>
<!-- 配置数据源自动实装 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ocss.dao" />
<property name="processPropertyPlaceHolders" value="true" />
<!-- <property name="sqlSessionTemplateBeanName" value="sqlSessionFactory" /> -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- 配置数据源事务控制 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 配置数据源连接池2 -->
<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName2}"/>
<property name="url" value="${jdbc.url2}"/>
<property name="username" value="${jdbc.username2}"/>
<property name="password" value="${jdbc.password2}"/>
<property name="maxActive" value="100"/>
<property name="initialSize" value="5"/>
<property name="minIdle" value="5"/>
<property name="maxWait" value="60"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="false"/>
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
</bean>
<!-- 配置数据源管理对象 -->
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<!-- <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> -->
<property name="mapperLocations" value="classpath:com/his/sqlmap/*.xml" />
<property name="typeAliasesPackage" value="com.his.dao"/>
</bean>
<!-- 配置数据源自动实装 -->
<bean id="mapperScanner2" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.his.dao" />
<property name="processPropertyPlaceHolders" value="true" />
<property name="sqlSessionTemplateBeanName" value="sqlSessionFactory2" />
<!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2" /> -->
</bean>
<bean id="sqlSession2" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory2" />
</bean>
<!-- 配置数据源事务控制 -->
<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource2" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager2" />
service如下
@Service
public class InfoContentServiceImpl implements InfoContentService {
@Autowired
//@Qualifier("sqlSessionFactory")
private InfoContentDao infoContentDao;