一个接口多个实现类的Spring注入方式

本文介绍如何在Spring框架中处理一个接口有多个实现类的情况,包括使用@Service注解的不同方式以及@Autowired和@Qualifier配合使用的方法。

1. 首先, Interface1 接口有两个实现类 Interface1Impl1 和 Interface1Impl2

Interface1 接口:

package com.example.service;

/**
 * Created by liuzh on 2018-05-29.
 * 接口1
 */
public interface Interface1 {
    void fun1();
}

以下是接口的两个实现类,请注意@service注解的使用方式,这里给每个实现类标注了不同的名称,方便在@Resource注入时区别注入

Interface1 接口实现类1:

package com.example.service.impl;

import com.example.service.Interface1;
import org.springframework.stereotype.Service;

/**
 * Created by liuzh on 2018-05-29.
 */
@Service("s1")
public class Interface1Impl1 implements Interface1 {
    @Override
    public void fun1() {
        System.out.println("接口1实现类 ...");
    }

    public void fun2(){
        System.out.println("接口1实现类1 fun2 ...");
    }
}

Interface1 接口实现类2:

package com.example.service.impl;

import com.example.service.Interface1;
import org.springframework.stereotype.Service;

/**
 * Created by liuzh on 2018-05-29.
 */
@Service("s2")
public class Interface1Impl2 implements Interface1 {
    @Override
    public void fun1() {
        System.out.println("接口1实现类 ...");
    }

    public void fun2(){
        System.out.println("接口1实现类2 fun2 ...");
    }
}

2. 通过 @Autowired 和 @Qualifier 配合注入

@Autowired
@Qualifier("interface1Impl1")
Interface1 interface1;    //正常启动

3. 使用@Resource注入,根据默认类名区分

@Resource(name = "interface1Impl1")
Interface1 interface1;    //正常启动

4. 使用@Resource注入,根据@Service指定的名称区分

@Resource(name = "s1")
Interface1 interface1;    //正常启动

使用@Resource注入,根据@Service指定的名称区分,可以避免多个实现类在不同包下,但是类名相同的情况。

感谢您的阅读,欢迎参观我的个人网站:小嗨词典【 https://www.happydict.cn】

Spring Boot 中,实现接口的类如果需要自动注入一个 `List` 类型的依赖,可以通过构造函数注入或者字段注入方式来实现。Spring 会自动将匹配的 Bean 注入到 `List` 类型的依赖中。 以下是一个示例,展示如何在实现类中自动注入 `List` 类型的依赖: ### 示例代码 假设有一个接口 `Service` 和多个实现类,这些实现类会被注入一个 `List` 中: ```java public interface Service { void execute(); } ``` 然后创建多个实现类: ```java @Component public class ServiceA implements Service { @Override public void execute() { System.out.println("Executing Service A"); } } @Component public class ServiceB implements Service { @Override public void execute() { System.out.println("Executing Service B"); } } ``` 接下来,在一个使用这些服务的类中,可以通过 `@Autowired` 自动注入一个 `List<Service>`: ```java @Component public class ServiceRunner { private final List<Service> services; @Autowired public ServiceRunner(List<Service> services) { this.services = services; } public void runAllServices() { for (Service service : services) { service.execute(); } } } ``` 在这个示例中,`ServiceRunner` 类通过构造函数接收一个 `List<Service>` 类型的依赖。Spring 会自动收集所有 `Service` 类型的 Bean,并将它们注入到 `services` 列表中。 ### 注意事项 1. **Bean 的作用域**:确保所有需要注入实现类都标记为 `@Component` 或其他 Spring Bean 注解,这样它们才能被 Spring 管理。 2. **依赖顺序**:注入的 `List` 中 Bean 的顺序可能不是固定的,如果需要特定的顺序,可以使用 `@Order` 注解或实现 `Ordered` 接口来定义顺序。 3. **泛型支持**:Spring 支持泛型注入,因此可以直接注入 `List<SomeType>`,而不需要额外的限定符[^1]。 ### 示例输出 当调用 `ServiceRunner.runAllServices()` 方法时,会依次执行 `ServiceA` 和 `ServiceB` 的 `execute()` 方法: ``` Executing Service A Executing Service B ``` 这种方式非常适合需要动态管理多个实现类的场景,例如插件系统、策略模式等。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值