获取接口的所有实现类

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

@Component
public class MyClass implements ApplicationContextAware {

    private ApplicationContext context;

    public void print() {
        Map<String, MyInterface> beanMap = context.getBeansOfType(MyInterface.class);
        for (String beanName : beanMap.KeySet()) {
            System.out.println(beanName + ": " + beanMap.get(beanName).xxx);  // xxx为接口的调用方法
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
}

猜你喜欢

转载自my.oschina.net/u/812874/blog/1527449