泛型依赖注入出现required a single bean, but 2 were found

摘要:
问题描述项目使用了tkmybatis,然后在service层使用泛型抽取了公共的CRUD操作,结果在Controller层注入的时候提示Fielddaoincom.gisquest.mmp.server.api.service.impl.BaseServiceImplrequiredasinglebean,but2werefound:-mmpDesignerAppMapper:definedinf

问题描述

项目使用了tkmybatis,然后在service层使用泛型抽取了公共的CRUD操作,结果在Controller层注入的时候提示

Field dao in com.gisquest.mmp.server.api.service.impl.BaseServiceImpl required a single bean, but 2 were found:
	- mmpDesignerAppMapper: defined in file [D:工作designerserver-javammp-server-api	argetclassescomgisquestmmpserverapimapperMmpDesignerAppMapper.class]
	- mmpDesignerAppReleaseMapper: defined in file [D:工作designerserver-javammp-server-api	argetclassescomgisquestmmpserverapimapperMmpDesignerAppReleaseMapper.class]

BaseService:

public interface BaseService<T extends BaseDomain> {
    int insert(T t,String createBy);

    int delete(T t);

    int update(T t,String updateBy);

    int count(T t);

    T selectOne(T t);

    PageInfo<T> page(int pageNum, int pageSize, T t);
}

AppService:

public interface AppService<T extends BaseDomain> extends BaseService<T> {
}

AppReleaseService:

public interface AppReleaseService<T extends BaseDomain> extends BaseService<T> {
    String getStatus(String id);
}

BaseServiceImpl:

@Service
@Transactional(readOnly = true)
public class BaseServiceImpl<T extends BaseDomain,D extends MyMapper<T>> implements BaseService<T> {

    @Autowired
    private D dao;

    @Override
    @Transactional(readOnly = false)
    public int insert(T t,String createBy) {
        t.setCreateBy(createBy);
        t.setCreateDate(new Date());
        return dao.insert(t);
    }

    @Override
    @Transactional(readOnly = false)
    public int delete(T t) {
        return dao.delete(t);
    }

    @Override
    @Transactional(readOnly = false)
    public int update(T t,String updateBy) {
        t.setUpdateBy(updateBy);
        t.setUpdateDate(new Date());
        return dao.updateByPrimaryKey(t);
    }

    @Override
    public int count(T t) {
        return dao.selectCount(t);
    }

    @Override
    public T selectOne(T t) {
        return dao.selectOne(t);
    }

    @Override
    public PageInfo<T> page(int pageNum, int pageSize,T t) {
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize);

        PageInfo<T> pageInfo = new PageInfo<>(dao.select(t));
        return pageInfo;
    }
}

AppServiceImpl:

@Service
@Transactional(readOnly = true)
public class AppServiceImpl extends BaseServiceImpl<MmpDesignerApp, MmpDesignerAppMapper> implements AppService<MmpDesignerApp> {
}

AppReleaseServiceImpl:

@Service
@Transactional(readOnly = true)
public class AppReleaseServiceImpl extends BaseServiceImpl<MmpDesignerAppRelease, MmpDesignerAppReleaseMapper> implements AppReleaseService<MmpDesignerAppRelease> {

    @Autowired
    private MmpDesignerAppReleaseMapper mapper;

    @Override
    public String getStatus(String id) {
        MmpDesignerAppRelease release = new MmpDesignerAppRelease();
        release.setAppid(id);
        int i = mapper.selectCount(release);
        if(i>0)
            return AppStatusConstants.RELEASED.getDesc();
        return AppStatusConstants.NOT_RELEASED.getDesc();
    }
}

AppController:

public class AppController {

    @Autowired
    private AppService<MmpDesignerApp> appService;

    @Autowired
    private AppReleaseService<MmpDesignerAppRelease> releaseService;

解决方法

将不能注入的bean也就是BaseServiceImpl变成抽象类
即在BaseServiceImpl类前加abstract

原因

Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException:
 Error creating bean with name 'baseServiceImpl':
  Unsatisfied dependency expressed through field 'dao';
   nested exception is
    org.springframework.beans.factory.NoUniqueBeanDefinitionException: 
   No qualifying bean of type 'tk.mybatis.mapper.MyMapper<?>' available:
    expected single matching bean but found 3:
     tbPostsPostMapper,tbSysUserMapper,tbPostsPostExtendMapper

也就是说在创建一个baseServiceImplbean放入容器时候,发现其类内的一个对象不能被明确指示,不能调用其方法。

ps: 参考文档

免责声明:文章转载自《泛型依赖注入出现required a single bean, but 2 were found》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Django中的缓存Elasticsearch集群角色类型node.master及node.data下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

spring security xml配置详解

security 3.x <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security"xmlns:beans="http://www.springframework.org/schem...

required a bean of type 'javax.sql.DataSource' that could not be found.

Description: Field dataSource in com.rjj.screw.ScrewApplication required a bean of type 'javax.sql.DataSource' that could not be found. The injection point has the following ann...

CAS 单点登陆

一、Tomcat配置SSL 1. 生成 server key 以命令方式换到目录%TOMCAT_HOME%,在command命令行输入如下命令:keytool -genkey -alias tomcat_key -keyalg RSA -storepass changeit -keystore server.keystore -validity 3600...

Java各种反射性能对比

对各种方法实现get方法的性能进行了一个测试。 总共有5个测试,,每个测试都是执行1亿次 1. 直接通过Java的get方法 2.通过高性能的ReflectAsm库进行测试 3.通过Java Class类自带的反射获得Method测试 4.使用Java自带的Property类获取Method测试 5.BeanUtils的getProperty测试 1 测试...

VueBlog

1、数据库建表时: timestamp有两个属性,分别是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP两种,使用情况分别如下: 1.CURRENT_TIMESTAMP  当要向数据库执行insert操作时,如果有个timestamp字段属性设为  CURRENT_TIMESTAMP,则无论这个字段有木有set值...

spring自动注入的三种方式

所谓spring自动注入,是指容器中的一个组件中需要用到另一个组件(例如聚合关系)时,依靠spring容器创建对象,而不是手动创建,主要有三种方式: 1. @Autowired注解——由spring提供 2. @Resource注解——由JSR-250提供 3. @Inject注解——由JSR-330提供   @Autowired注解的使用方法 @Tar...