package com.prolog.cs.biz.inbound.utils;

import com.google.common.collect.Lists;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author spring 操作类
 */
@Component
public class SpringUtils implements ApplicationContextAware {

	static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext){
		SpringUtils.applicationContext = applicationContext;
	}

	public static <T> Map<String, T> getBeansByInterface(Class<T> inf) {
		return applicationContext.getBeansOfType(inf);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBeansByName(String name) {
		return (T) applicationContext.getBean(name);
	}

	public static <T> T getBeansByType(Class<T> clz) {
		return applicationContext.getBean(clz);
	}

	public static <T> List<T> getAllBeansByInterface(Class<T> bo) {
		Map<String, T> map = SpringUtils.getBeansByInterface(bo);
		List<T> implList = Lists.newArrayList();
		map.forEach((key, v) -> {
			implList.add(v);
		});
		return implList;
	}
}
