问题描述:一个接口,有俩个实现类当注入时候名字不同时,会出现不同的情况
action层:
1 @Controller("userAction") 2 @Scope("prototype") 3 public class UserAction extends ActionSupport implements ModelDriven<User>{ 4 private User user; 5 private IUserService userService; 6 7 public IUserService getUserService() { 8 return userService; 9 } 10 11 @Resource 12 public void setUserService(IUserService userService) { 13 this.userService = userService; 14 } 15 16 17 public User getUser() { 18 return user; 19 } 20 21 public void setUser(User user) { 22 this.user = user; 23 } 24 //model 25 public User getModel() { 26 if(user == null) { 27 user = new User(); 28 } 29 return user; 30 } 31 //列表 32 public String list(){ 33 List<User> us = userService.listAllUser(); 34 ActionContext.getContext().put("us", us); 35 return SUCCESS; 36 } 37 38 }
接口层
public interface IUserService { public List<User> listAllUser(); }
实现类
(一)
@Service("userService") public class UserService1 implements IUserService { public List<User> listAllUser() { System.out.println("user1-------------------------"); return null; } }
(二)
@Service("aaa") public class UserService2 implements IUserService { public List<User> listAllUser() { System.out.println("user2-------------------------"); return null; } }
(三)
@Service("bbb") public class UserService3 implements IUserService { public List<User> listAllUser() { System.out.println("user3-------------------------"); return null; } }
(四)
@Service("bbb") public class UserService4 implements IUserService { public List<User> listAllUser() { System.out.println("user4-------------------------"); return null; } }
情况一:当实现类为(一)和其他实现类的时候,只实现(一)
注意:此时 @Service("userService")和action中private IUserService userService名字一样
情况二:当实现类为(二)和(三)的时候,报错
No unique bean of type [com.fly.service.IUserService] is defined: expected single matching bean but found 2: [aaa, bbb]
情况三:当实现类为(四)和(三)的时候,启动服务就报错
情况四:当实现类只有一个的时候,正常执行