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

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

1接口

package com.example.demo.service;

public interface UserService {

    void saveUser();
}

2实现类

实现类1

package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service("userServiceImpl1")
public class UserServiceImpl implements UserService{

    @Override
    public void saveUser() {
        System.out.println("测试实现1");
    }
}

实现类2

package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service("userServiceImpl2")
public class UserServiceImpl2 implements UserService{

    @Override
    public void saveUser() {
        System.out.println("测试实现2");
    }
}

3进行调用

controller层调用

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

package com.example.demo.controller;

import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("save")
    public String saveUser(){

        userService.saveUser();

        return "save ok";
    }
}

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

image-20210725114912153

3.1使用@Resource

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

package com.example.demo.controller;

import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class UserController {

    @Resource(name = "userServiceImpl1")
    private UserService userService;

    @RequestMapping("save")
    public String saveUser(){

        userService.saveUser();

        return "save ok";
    }
}

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

image-20210725140216626

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

image-20210725140325122

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

image-20210725140356448

3.2使用@Autowired和@Qualifier

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

package com.example.demo.controller;

import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class UserController {

    @Autowired
    @Qualifier("userServiceImpl1")
    private UserService userService;

    @RequestMapping("save")
    public String saveUser(){

        userService.saveUser();

        return "save ok";
    }
}

image-20210725140843544

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

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

image-20210725140916041

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值