Could not autowired,There is more than one bean of " xxxxxx"

前几天刚把新建的项目跑通,领导让我还用原来的框架,于是我又照着原来的配置文件新建一个项目,再次跑通,今早来,想把原来框架里那些东西拖进来,结果service实现类报了这个错,意思就是有两个名为“xxxxxx”的mapper层的bean,注入失败了。

后来一想,好像是配置文件配置过对mapper层的扫描了,应该是哪里又按照ByName的方式扫描了一遍,脑子突然嗡嗡的,那特么肯定就是注解啊,自动扫描,按照ByName自动注入,一看果然是!代码如下:

package cn.tdhc.imip.cwsfd.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import cn.tdhc.imip.cwsfd.entity.CwCollection;

@Repository("cwCollectionMapper")//括号里的内容就是指定的要按照ByName注入的mapper层实例
public interface CwCollectionMapper {
    
    int deleteByPrimaryKey(String colId);

    int insert(CwCollection record);

    int insertSelective(CwCollection record);

    CwCollection selectByPrimaryKey(String colId);

    int updateByPrimaryKeySelective(CwCollection record);

    int updateByPrimaryKey(CwCollection record);

   List<CwCollection> M1S11Q(CwCollection cwCollection);

   int M1S11A(@Param("list") List<CwCollection> collectionList);

   int M1S11U(@Param("list") List<CwCollection> collectionList);

   int M1S11D(@Param("list") List<CwCollection> collectionList);

   int M1S11T(@Param("list") List<CwCollection> collectionList);

   int M1S11B(@Param("list") List<CwCollection> collectionList);

   List<CwCollection> M1S11Q_SS(CwCollection cwCollection);

   List<CwCollection> M1S11Q_QXN(CwCollection cwCollection);
}

在注解后的括号里就已经指定了mapper层的bean名称,然后再看配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="tdhc_impl_sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定mybatis全局配置文件的位置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="dataSource" ref="tdhc_impl_dataSource"/>
        <!-- 指定mybatis,mapper文件的位置 -->
        <property name="mapperLocations">
            <list>
                <value>classpath:cn/tdhc/imip/cwsfd/sql/*.xml</value>
            </list>
        </property>
    </bean>
    <!--  扫描mapper层  -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.tdhc.imip.cwsfd.mapper"/>
    </bean>
</beans>

在最后又把mapper扫描了一遍,所以才会有两个名字相同的mapper层的bean,解决方法有两个,要么去掉注解后面的内容,要么去掉扫描mapper层的配置。

发布了15 篇原创文章 · 获赞 2 · 访问量 808

猜你喜欢

转载自blog.csdn.net/oak_javaLearner/article/details/103869500