Spring解决同一个接口不同实现类的依赖注入的方式

本文介绍了在Spring框架中如何处理同一个接口有两个不同实现类的情况,包括使用@Service、@Primary、@Configuration和@Bean等注解进行依赖注入,并演示了如何通过@Autowired、@Qualifier和@Resource解决bean选择问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值