zoukankan      html  css  js  c++  java
  • 一个接口有2个不同的实现,如何Autowire某一个指定的实现

    一个接口

    public interface AServlce{

      public ADao getAId(Long id);

    }

    俩个实现

    @Service("service")

    public class AImpl implements AServlce{

      public ADao getAId(Long id){

        return new ADao();

      }

    }

    @Service("service1")

    public class AImpl implements  AServlce{

      public ADao getAId(Long id){

        return new ADao();

      }

    }

    调用代码

    @Controller

    @RequestMapping("/")

    public class AControl{

      @AutoWired

      AServlce aServlce;

      @RequestMapping("/")

      public void fun(HttpServletRequest request,HttpServletResponse response){

        略...

      }

    }

    这样是错误的,该接口不知道映射哪个实现类。所以用到@Qualifier注解。

    @Controller

    @RequestMapping("/")

    public class AControl{

      @AutoWired

      @Qualifier("service")

      AServlce aServlce;

      @RequestMapping("/")

      public void fun(HttpServletRequest request,HttpServletResponse response){

        略...

      }

    }

  • 相关阅读:
    Python 特点
    Python简介
    数据库查询语句
    人月神话读书笔记01
    团队介绍
    团队项目一 原型展示+电梯演讲
    全球疫情可视化展示
    NABCD模型
    第六周学习进度
    构建之法阅读笔记03
  • 原文地址:https://www.cnblogs.com/vlsion/p/8668440.html
Copyright © 2011-2022 走看看