注解装配bean(四)——消除自动装配歧义性

注解装配bean(一)

注解装配bean(二)

注解装配bean(三)

在自动装配中存在歧义性问题:当某个接口由多个类实现时,无法确定自动装配的是哪个类

案例

(1)还是service接口,本次我们使用两个类来实现该接口

RoleServiceImpl是RoleService的一个实现类

package com.ssm.spring.annotation.service.impl;

import com.ssm.spring.annotation.pojo.Role;
import com.ssm.spring.annotation.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component("RoleService")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private Role role = null;

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    @Override
    public void printRoleInfo() {
        System.out.println("id ="+role.getId());
        System.out.println("roleName ="+role.getRoleName());
        System.out.println("note ="+role.getNote());
    }
}

RoleServiceImpl2也是RoleService的一个实现类

package com.ssm.spring.annotation.service.impl;

import com.ssm.spring.annotation.pojo.Role;
import com.ssm.spring.annotation.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("RoleService2")
public class RoleServiceImpl2 implements RoleService {
    @Autowired
    private Role role = null;

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    @Override
    public void printRoleInfo() {
        System.out.println("id ="+role.getId());
        System.out.println("roleName ="+role.getRoleName());
        System.out.println("note ="+role.getNote());
    }
}

(2)创建RoleController类,该类中使用@Autowired注解自动装配RoleService

package com.ssm.spring.annotation.controller;

import com.ssm.spring.annotation.pojo.Role;
import com.ssm.spring.annotation.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class RoleController {
    @Autowired
    @Qualifier("RoleService")
    private RoleService roleService = null;

    public void printRole()
    {
        roleService.printRoleInfo();
    }
}

(3)创建java config,本次需要向Ioc容器中注入4个bean,分别是Role、RoleServiceImpl、RoleServiceImpl2、以及RoleController。Role的代码与前文相同,RoleServiceImpl和RoleServiceImpl2中自动装配Role,RoleController自动装配RoleService。java config代码:

package com.ssm.spring.annotation.config;

import com.ssm.spring.annotation.controller.RoleController;
import com.ssm.spring.annotation.pojo.Role;
import com.ssm.spring.annotation.service.impl.RoleServiceImpl;
import com.ssm.spring.annotation.service.impl.RoleServiceImpl2;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackageClasses = {Role.class, RoleServiceImpl.class, RoleController.class, RoleServiceImpl2.class})
public class ApplicationConfig {
}

(4)编写测试代码

package com.ssm.spring;

import com.ssm.spring.annotation.config.ApplicationConfig;
import com.ssm.spring.annotation.controller.RoleController;
import com.ssm.spring.annotation.service.RoleService;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class test {
    @Test
    public void test1()
    {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        RoleController controller = context.getBean(RoleController.class);
        controller.printRole();
        context.close();
    }
}

测试结果报错,向RoleController自动装配RoleService时,由于该接口有两个实现类,匹配bean时找到两个,此时自动装备不知道装配哪个,就报错了。

 修改代码:

注解@Qualifier能过够消除该歧义性,在@Qualifier后面添加具体哪个bean的id名字即可

修改后的RoleController代码如下:在自动装配@Autowired下面添加注解@Qualifier并指定哪个bean即可消除歧义性

package com.ssm.spring.annotation.controller;

import com.ssm.spring.annotation.pojo.Role;
import com.ssm.spring.annotation.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class RoleController {
    @Autowired
    @Qualifier("RoleService")
    private RoleService roleService = null;

    public void printRole()
    {
        roleService.printRoleInfo();
    }
}

再次运行测试代码:

 成功输出注入的Role实例

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值