在自动装配中存在歧义性问题:当某个接口由多个类实现时,无法确定自动装配的是哪个类
案例
(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实例