Spring动态获取接口的所有实现类

一个接口有多个实现类 , 通过实现ApplicationContextAware将所有的实现类管理起来 , 使用的时候传入实现类的容器名称调用。

@Autowired
private InterfaceServiceLocator locator;

// 获取接口指定的实现类( Spring容器中默认名称是类名首字母小写 )
InterfaceService service = locator.getService("interfaceServiceImpl1");
// 接口
public interface InterfaceService {

}
// 实现类1
public class InterfaceServiceImpl1 implements InterfaceService {

}
// 实现类2
public class InterfaceServiceImpl2 implements InterfaceService {

}
package cn.com.baidu.filter;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

/**
 * explain:获取实现类
 * @author cbc
 * @date 2021/1/5
 */
@Component
public class InterfaceServiceLocator implements ApplicationContextAware {

    /**
     * 用于保存接口实现类名及对应的类
     */
    private Map<String, InterfaceService> map = new HashMap<String, InterfaceService>();

    /**
     * 获取应用上下文并获取相应的接口实现类
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //根据接口类型返回相应的所有bean
        map = applicationContext.getBeansOfType(InterfaceService.class);
    }

    /**
     * 获取所有实现集合
     * @return
     */
    public Map<String, IShopAuditLogUpdateService> getMap() {
        return map;
    }

    /**
     * 获取对应实现类
     * @param key
     * @return
     */
    public IShopAuditLogUpdateService getService(String key) {
        return map.get(key);
    }

}

Spring框架中,如果你想获取某个接口下面的所有实现,可以使用Spring的依赖注入功能结合Java的反射机制来实现。以下是一种常见的实现方式: 1. 利用Spring的ApplicationContext获取所有的Bean信息。 2. 遍历这些Bean,检查它们是否是目标接口实现。 3. 如果是,则将这些实现收集起来。 具体步骤如下: 首先,你可以定义一个方法,该方法返回型为List<Class<?>>,用于存放所有的实现。在这个方法中,你需要获取ApplicationContext中的所有BeanDefinition,然后使用ClassUtils得到每个Bean对应的Class对象,最后通过instanceof操作符检查这些Class对象是否是目标接口实现。 示例代码如下: ```java import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import java.util.ArrayList; import java.util.List; import java.util.Map; public class SpringBeanUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringBeanUtil.applicationContext = applicationContext; } public static List<Class<?>> getBeanClassesForInterface(Class<?> interfaceClass) { Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(Component.class); List<Class<?>> implementClasses = new ArrayList<>(); for (Object bean : beansWithAnnotation.values()) { Class<?> beanClass = bean.getClass(); if (interfaceClass.isAssignableFrom(beanClass) && !beanClass.isInterface()) { implementClasses.add(beanClass); } } return implementClasses; } } ``` 在上面的代码中,`getBeanClassesForInterface`方法将遍历所有带有@Component注解的Bean(这意味着所有Spring管理的Bean),检查它们是否实现了目标接口。如果是,则将它们的Class对象添加到`implementClasses`列表中。 请注意,这个示例假设所有Bean都带有@Component(或其衍生注解如@Service、@Repository等)注解。如果Bean的定义方式不同,可能需要调整获取Bean的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值