Spring解决同一个接口不同实现类的依赖注入的方式:
一.声明公共接口:
二.定义两个不同的实现类:
三.测试
1.使用@Service或者@Component去装配bean:
2:使用@Service+@Primary装配bean
3:使用@Configuration的配置类和@Bean的方式装配bean
一.声明公共接口:
import com.packet.cheng.entity.Person;
import java.util.List;
public interface PersonService {
List<Person> selectPersonInfo();
}
二.定义两个不同的实现类:
import com.packet.cheng.entity.Person;
import com.packet.cheng.service.PersonService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
//@Service
public class ManPersonServiceImpl implements
PersonService {
@Override
public List<Person> selectPersonInfo() {
List<Person> personList = new ArrayList<>();
Person person = new Person(1,"张三","男",45);
Person person1 = new Person(2,"李四","男",40);
Person person2 = new Person(3,"成龙","男",70);
Person person3 = new Person(4,"王麻子","男",90);
personList.add(person);
personList.add(person1);
personList.add(person2);
personList.add(person3);
return personList;
}
}
import com.packet.cheng.entity.Person;
import com.packet.cheng.service.PersonService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
//@Service
public class WomanPersonServiceImpl implements PersonService {
@Override
public List<Person> selectPersonInfo() {
List<Person> personList = new ArrayList<>();
Person person = new Person(1,"李美","女",30);
Person person1 = new Person(2,"王丽","女",19);
Person person2 = new Person(3,"思颖","女",29);
Person person3 = new Person(4,"可欣","女",33);
personList.add(person);
personList.add(person1);
personList.add(person2);
personList.add(person3);
return personList;
}
}
三.测试:
1.使用@Service或者@Component去装配bean:
(1).选择@AutoWired实现依赖注入:
1.直接使用:@Autowired
@Autowired
private PersonService personService;
@Test
public void testImplSameInterfaceOfTwoBean(){
List<Person> personList = personService.selectPersonInfo();
personList.forEach(person -> System.out.println(person));
}
这样测试会报错找到了匹配的两个bean
2.@AutoWired上添加@Qualifier注解,指定beanName
@Autowired
@Qualifier(value = "manPersonServiceImpl") // 加入@Qualifier注解,指定spring容器默认装配bean的名称
private PersonService personService;
@Test
public void testImplSameInterfaceOfTwoBean(){
List<Person> personList = personService.selectPersonInfo();
personList.forEach(person -> System.out.println(person));
}
(2).使用@Resource注解,通过名字和类型进行依赖注入:
1.指定beanName
@Resource(name="指定对应的bean的名称")
private PersonService personService;
2.指定类型
@Resource(type=类的名称.class)
private PersonService personService;
3.测试:
@Test
public void testImplSameInterfaceOfTwoBean(){
List<Person> personList = personService.selectPersonInfo();
personList.forEach(person -> System.out.println(person));
}
2:使用@Service+@Primary装配bean
1. @Service注解加入@Primary注解
@Autowired
private PersonService personService;
@Test
public void testImplSameInterfaceOfTwoBean(){
List<Person> personList = personService.selectPersonInfo();
personList.forEach(person -> System.out.println(person));
}
3:使用@Configuration的配置类和@Bean的方式装配bean
import com.packet.cheng.service.PersonService;
import com.packet.cheng.service.impl.ManPersonServiceImpl;
import com.packet.cheng.service.impl.WomanPersonServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PoJoConfig {
@Bean
public PersonService manPersonService(){
return new ManPersonServiceImpl();
}
@Bean
public PersonService womanPersonService(){
return new WomanPersonServiceImpl();
}
}
@Autowired // 直接使用会报找到两个bean
//@Qualifier(value = "bean的名字")
//@Resource(name = "bean的名字")
private PersonService personService;
@Test
public void testImplSameInterfaceOfTwoBean(){
List<Person> personList = personService.selectPersonInfo();
personList.forEach(person -> System.out.println(person));
}