更多获取方式看 spring的获取容器中的bean
按名字, 注解, 继承, 接口等等各种方式获取
定义
@Service("commandToolFactory")public class CommandToolFactory implements ApplicationContextAware, InitializingBean {private Map<String, CommandToolHandler> handlerMap;private ApplicationContext applicationContext;@Overridepublic void afterPropertiesSet() throws Exception {final Map<String, CommandToolHandler> handlerMap =this.applicationContext.getBeansOfType(CommandToolHandler.class);this.handlerMap = handlerMap;}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}public CommandToolHandler getToolHandler(String cate) {return this.handlerMap.get(cate + "ToolHandler");}}
目标的类是 NormalToolHandler
使用
@Autowiredprivate CommandToolFactory commandToolFactory;CommandToolHandler handler = this.commandToolFactory.getToolHandler(orderType.getKey());
