首页 > 其他 > 详细

@Qualifier 注解指定装备特定的bean

时间:2020-08-25 14:51:18      阅读:105      评论:0      收藏:0      [点我收藏+]

 

 

 

public interface TestService {
    String test();
}


@Service("aTestService")
public class ATestServiceImpl implements TestService{

    @Override
    public String test() {
        return "A";
    }
}


@Service("bTestService")
public class BTestServiceImpl implements TestService {

    @Override
    public String test() {
        return "B";
    }
}

 

测试

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    TestServiceImpl testServiceImpl;

    @RequestMapping("/a")// localhost:8080//test/a?type=0
    public String test(int type){
        return testServiceImpl.test(type);
    }
}
@Service
public class TestServiceImpl {

    @Autowired
    @Qualifier("aTestService")
    TestService aTestService;

    @Autowired
    @Qualifier("bTestService")
    TestService bTestService;

    public String  test(int type){
        if (0==type) {
            return aTestService.test();
        }else{
            return bTestService.test();
        }
    }
}

结果:

http://localhost:8080//test/a?type=0   结果为A

http://localhost:8080//test/a?type=1   结果为B

 

@Qualifier 注解指定装备特定的bean

原文:https://www.cnblogs.com/yrjns/p/13559429.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!