首页 > 其他分享 >Spring注入接口,接口多个实现类调用哪个实现类的三种方案

Spring注入接口,接口多个实现类调用哪个实现类的三种方案

时间:2023-04-26 09:55:36浏览次数:33  
标签:Autowired 实现 Spring 接口 springframework import org annotation UserService

https://blog.csdn.net/JingXu1114/article/details/124747047

代码所示:

··· @Autowired UserService userService ···
在这个接口有多个实现类的情况下三种方式定义调用实现类:

方法1:
··· `@Autowired
UserService userServiceImpl_1` ···
在变量名中直接写成想要调用的那个实现类名称,名称规定为首字母小写

方法2
@Primary 注解,标注在实现类上,注解意思:优先使用本类。

方法3
结合使用:
只需要在注⼊的时候,额外通过另外⼀个注解@Qualifier来标识需要注⼊实现类的名字
@Autowired
@Qualifier("userServiceImpl_1")
UserService userService;

同理:@Resource跟方法三是一样的原理
方法一是根据类的BeanName在容器中跟beanId对比寻找,
方法二是直接表示用哪个。
方法三是直接根据beanId找全限定路径找类。
在这里推荐使用@Resource,方便快捷。

在spring中出现,一个接口有两个实现类,怎么调用?

https://blog.csdn.net/qq_32940105/article/details/125717957?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-125717957-blog-124747047.235%5Ev32%5Epc_relevant_default_base3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-125717957-blog-124747047.235%5Ev32%5Epc_relevant_default_base3&utm_relevant_index=2

 

在spring中出现,一个接口有两个实现类,怎么调用?
1接口

 

  1.   package com.example.demo.service;
  2.    
  3. public interface UserService {
  4.       void saveUser();
  5.   }


2实现类
实现类1

  1.   package com.example.demo.service;
  2.    
  3.   import org.springframework.stereotype.Service;
  4.    
  5.   @Service("userServiceImpl1")
  6.   public class UserServiceImpl implements UserService{
  7.    
  8.       @Override
  9.       public void saveUser() {
  10.           System.out.println("测试实现1");
  11.       }
  12.   }


实现类2

  1.   package com.example.demo.service;
  2.    
  3.   import org.springframework.stereotype.Service;
  4.    
  5.   @Service("userServiceImpl2")
  6.   public class UserServiceImpl2 implements UserService{
  7.    
  8.       @Override
  9.       public void saveUser() {
  10.           System.out.println("测试实现2");
  11.       }
  12.   }


3进行调用
controller层调用

使用@Autowired的注入方式进行注入,@Autowired的注入方式是byType注入,当要注入的类型在容器中存在多个时,Spring是不知道要要入哪个实现类,所以会报错。

  1.   package com.example.demo.controller;
  2.    
  3.   import com.example.demo.service.UserService;
  4.   import org.springframework.beans.factory.annotation.Autowired;
  5.   import org.springframework.web.bind.annotation.RequestMapping;
  6.   import org.springframework.web.bind.annotation.RestController;
  7.    
  8.   @RestController
  9.   public class UserController {
  10.    
  11.       @Autowired
  12.       private UserService userService;
  13.    
  14.       @RequestMapping("save")
  15.       public String saveUser(){
  16.    
  17.           userService.saveUser();
  18.    
  19.           return "save ok";
  20.       }
  21.   }


启动直接报错,spring是单例的但是找到了两个实例。

3.1使用@Resource
使用**@Resource默认是按照byName的方式注入的**,如果通过byName的方式匹配不到,再按byType的方式匹配

  1.   package com.example.demo.controller;
  2.    
  3.   import com.example.demo.service.UserService;
  4.   import org.springframework.beans.factory.annotation.Autowired;
  5.   import org.springframework.web.bind.annotation.RequestMapping;
  6.   import org.springframework.web.bind.annotation.RestController;
  7.    
  8.   import javax.annotation.Resource;
  9.    
  10.   @RestController
  11.   public class UserController {
  12.    
  13.       @Resource(name = "userServiceImpl1")
  14.       private UserService userService;
  15.    
  16.       @RequestMapping("save")
  17.       public String saveUser(){
  18.    
  19.           userService.saveUser();
  20.    
  21.           return "save ok";
  22.       }
  23.   }


访问:http://localhost:8080/save

根据打印结果可以看到注入的是userServiceImpl1

更改为userServiceImpl2,打印的是测试实现2

3.2使用@Autowired和@Qualifier
@Qualifier注解也是byName的方式,是直接按照名字进行搜索,对于UserServiceImpl上面@Service注解必须写名字,不写就会报错,而且名字必须是@Autowired @Qualifie("userServiceImpl")保持一致。
 

  1.   package com.example.demo.controller;
  2.    
  3.   import com.example.demo.service.UserService;
  4.   import org.springframework.beans.factory.annotation.Autowired;
  5.   import org.springframework.beans.factory.annotation.Qualifier;
  6.   import org.springframework.web.bind.annotation.RequestMapping;
  7.   import org.springframework.web.bind.annotation.RestController;
  8.    
  9.   import javax.annotation.Resource;
  10.    
  11.   @RestController
  12.   public class UserController {
  13.    
  14.       @Autowired
  15.       @Qualifier("userServiceImpl1")
  16.       private UserService userService;
  17.    
  18.       @RequestMapping("save")
  19.       public String saveUser(){
  20.    
  21.           userService.saveUser();
  22.    
  23.           return "save ok";
  24.       }
  25.   }


浏览器访问:http://localhost:8080/save

可以看到用的是测试实现1

 

标签:Autowired,实现,Spring,接口,springframework,import,org,annotation,UserService
From: https://www.cnblogs.com/zhoading/p/17354759.html

相关文章

  • P.19-token认证过滤器代码实现、P.20-配置认证过滤器、P.21-退出登录
    P.19-token认证过滤器代码实现自定义一个过滤器,这个过滤器会去获取请求头中的token,对token进行解析取出其中的userid。使用userid去redis中获取对应的LoginUser对象。然后封装Authentication对象存入SecurityContextHolder@ComponentpublicclassJwtAuthentica......
  • SpringBoot的自动装配原理
    前言因为最近在准备知识框架的学习,对springboot的自动装配原理学习了一番,也看了一些视频和一些博客,根据自己的理解在此记录一下,有什么理解得不对的,可以指出一起讨论。本文主要根据以下的几个点对自动装配原理进行介绍:什么是springboot的自动装配springboot的自......
  • JWT 实现登录认证 + Token 自动续期方案 转载
    过去这段时间主要负责了项目中的用户管理模块,用户管理模块会涉及到加密及认证流程,加密已经在前面的文章中介绍了。今天就来讲讲认证功能的技术选型及实现。技术上没啥难度当然也没啥挑战,但是对一个原先没写过认证功能的菜鸡甜来说也是一种锻炼吧。技术选型要实现认证功能,很容易......
  • 滑动窗口算法实现分布式第三方请求限频
    一.业务背景 第三方服务接口存在频率调用限制(例如,1s5次,超过5次返回超出频率),己方服务存在并发处理的情况,为了保证服务的成功率,且达到第三方限制的最大吞吐量,故需要一个限频调用的算法二.实现思路常见限频算法一般有五种,漏桶算法、令牌桶算法、固定窗口算法,滑动窗口算法,漏斗算......
  • Spring Boot 项目代码混淆,实战来了,再也不用担心代码泄露了!
    编译简单就是把代码跑一哈,然后我们的代码.java文件就被编译成了.class文件反编译就是针对编译生成的jar/war包里面的.class文件逆向还原回来,可以看到你的代码写的啥。比较常用的反编译工具JD-GUI,直接把编译好的jar丢进去,大部分都能反编译看到源码:那如果不想给......
  • 基于FPGA的DDS设计,并通过DDS实现ASK,FSK,PSK三种调制
    1.算法仿真效果matlab2013b+QUARTUS7.2仿真结果如下:    然后使用DDS产生的sin曲线进行ASK,FSK,PSK调制,结果如下:    2.算法涉及理论知识概要        随着现代电子技术的不断发展,很多应用领域对信号的频率的准确度和稳定性要求越来越高,不仅需要......
  • SMU Spring 2023 Trial Contest Round 10
    SMUSpring2023TrialContestRound10 A-RemoveDuplicates#include<bits/stdc++.h>usingnamespacestd;typedefpair<int,int>PII;typedefpair<string,int>PSI;constintN=2e2+5,INF=0x3f3f3f3f,Mod=1e6;constdoubleeps=1e-6;typedef......
  • m基于LOC-PCA算法的人脸重建算法matlab仿真,给定人物侧脸实现正脸重建
    1.算法仿真效果matlab2022a仿真结果如下:       2.算法涉及理论知识概要      提出了一种有效的图像姿态合成方法。姿势合成用于预测在给定姿势的期望姿势处具有最小误差的面部图像。在许多情况下,这是经常需要的例如动画电影的制作、法医学和3D人脸几......
  • SQL Server实现group_concat功能的详细实例
    目录一、实现二、原理分析2.1、FOR XML PATH的作用2.2、STUFF函数2.2.1、STUFF函数在本SQL的作用2.2.2、STUFF函数语法2.3、sql语分分析2.3.1、一个简单的group by2.3.2、在select语句后面加上子查询2.3.3、去掉子查询结果集的第一个分隔符总结一、实......
  • SpringBoot 第一个demo
    前奏最近在面试,有一家公司在谈的时候,发了一份后端笔试题给我,是java的......我TMD是个运维诶,你给我一套SRE题不行嘛......玛德现在都这么卷了吗,SRE要去卷java啦...... SpringBoot对于Java的很多东西我并不懂,但是我知道写java的后端都会用到Spring全家桶,包括SpringBoot、Spri......