package com.manage.config; import com.manage.interfaces.webservice.PowerWebService; import org.springframework.beans.BeansException; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; /** * @author by CLP * @Classname BeanConfig * @Description * @Date 2020/9/8 15:28 */ @Component public class WebServiceCxf implements InitializingBean, ApplicationContextAware { private static Map queryServiceImplMap = new HashMap<>(); private ApplicationContext applicationContext; public static PowerWebService createQueryService(String type) { PowerWebService powerWebService = queryServiceImplMap.get(type); if (powerWebService == null) { return queryServiceImplMap.get("PowerServiceImpl"); } return powerWebService; } @Override public void afterPropertiesSet() throws Exception { Map beanMap = applicationContext.getBeansOfType(PowerWebService.class); //遍历该接口的所有实现,将其放入map中 for (PowerWebService serviceImpl : beanMap.values()) { queryServiceImplMap.put(serviceImpl.getClass().getSimpleName(), serviceImpl); } } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }