项目中使用了大量的工厂类,采用了简单工厂模式:

通过该工厂类可以获取指定的处理器bean,这些处理器bean我们是从spring容器中获取的,如何获取,是通过实现ApplicationContextAware接口完成的。

@Component("handlerFactory")
public class HandlerFactory implements InitializingBean, ApplicationContextAware { private static final Map<String, HandlerIntf> HANDLER_MAP = new HashMap<String, HandlerIntf>(); private ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} /**
* 初始化工厂
*
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
// 处理器1
HANDLER_MAP.put(TaskTypeConstants.RECON_DATA, applicationContext.getBean("handle1", HandlerIntf.class)); // 处理器2
HANDLER_MAP.put(TaskTypeConstants.BUSINESS_DATA, applicationContext.getBean("handle2", HandlerIntf.class));
} /**
* 根据数据类型获取处理器
*
* @param dataType
* @return
*/
public SettleRequestHandlerIntf getHandler(String dataType) {
HandlerIntf handler = HANDLER_MAP.get(dataType);
if (null == handler) {
throw new AppException("0001", dataType + "不存在对应结算明细处理器");
}
return handler;
}
}

1. 实现setApplicationContext方法

通过以上代码,可以看到继承了ApplicationContextAware接口,就要实现setApplicationContext方法,而方法中applicationContext,是spring容器注入进来的,这样我们就可以通过applicationContext变量获取spring容器中存在的bean。

2. 在spring中注入当前HandlerFactory类
刚才我们提到spring容器会自动将applicationContext注入进来,那么spring容器怎么识别到当前HandlerFactory类的?原因就在于HandlerFactory类加了注解:

@Component("handlerFactory")

或这种方式注入:

<bean id="handlerFactory" class="com.xxx.HandlerFactory"/>

这样spring才能扫描到它,并将applicationContext注入。

spring中ApplicationContextAware接口描述的更多相关文章

  1. spring中ApplicationContextAware接口使用理解

    一.这个接口有什么用?当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以直 ...

  2. Spring中ApplicationContextAware接口的说明

    转载 https://www.cnblogs.com/muqingzhi123/p/9805623.html 1.为什么使用AppplicationContextAware? ApplicationC ...

  3. Spring中ApplicationContextAware接口的用法

    1.为什么使用AppplicationContextAware? ApplicationContext的BeanFactory 的子类, 拥有更强大的功能,ApplicationContext可以在服 ...

  4. Spring中Ordered接口简介

    目录 前言 Ordered接口介绍 Ordered接口在Spring中的使用 总结 前言 Spring中提供了一个Ordered接口.Ordered接口,顾名思义,就是用来排序的. Spring是一个 ...

  5. 转:spring中InitailizingBean接口的简单理解

    转自:https://www.cnblogs.com/wxgblogs/p/6849782.html spring中InitializingBean接口使用理解   InitializingBean接 ...

  6. web 工程中利用Spring的 ApplicationContextAware接口自动注入bean

    最常用的办法就是用 ClassPathXmlApplicationContext, FileSystemClassPathXmlApplicationContext, FileSystemXmlApp ...

  7. Spring中ApplicationContextAware的用法

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt379 一.这个接口有什么用? 当一个类实现了这个接口(Application ...

  8. Spring的ApplicationContextAware接口的作用

    ApplicationContextAware接口: 当一个类实现了这个接口之后,这个类就可以方便地获得 ApplicationContext 中的所有bean.换句话说,就是这个类可以直接获取Spr ...

  9. Spring中ApplicationContextAware的作用

    ApplicationContextAware 通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法. ...

随机推荐

  1. IDEA中maven的依赖jar包报红

    问题描述: 查看本地的repository中有相关的jar包,但是在IDEA中就是报红(下面红色波浪线) 解决方法: 将pom.xml中相关的dependcy配置注释掉,maven中jar包就会删除. ...

  2. SharePoint REST API - 列表和列表项

    博客地址:http://blog.csdn.net/FoxDave 本篇主要讲述如何用SharePoint REST操作列表和列表项.阅读本篇时请先了解前面讲述的REST介绍和基本操作. 废话不多 ...

  3. layer 问题 汇总

    1.搭配iframe  子页面遮罩层 覆盖父页面 window.parent.layer.open({      // type: 1, //skin: 'layui-layer-rim', //加上 ...

  4. 记一次给nginx的web服务器目录加软链接

    先期情况和问题 已安装好nginx服务器和相关配置.nginx的web目录是/usr/share/nginx/html. 现在的问题是希望把web目录换成/root/nginx/html/,但是不更改 ...

  5. prinft he sprintf

    四.printf函数 printf函数返回一个格式化后的字符串. 语法:printf(format,arg1,arg2,arg++) 参数 format 是转换的格式,以百分比符号 (“%”) 开始到 ...

  6. 整理有关浏览器兼容性的css样式

    去掉IE自带的删除功能的×号 input::-ms-clear{display:none;} 去掉IE自带密码框的眼睛样式 input::-ms-reveal{display:none;}

  7. [转]How rival bots battled their way to poker supremacy

    How rival bots battled their way to poker supremacy http://www.nature.com/news/how-rival-bots-battle ...

  8. ubuntu discuz 该函数需要 PHP 支持 XML。请联系空间商,确定开启了此项功能

    apt-get install php-xml apt-get install php-xml-parser

  9. triplet改进,变种

    1.一开始是FaceNet 2.一个重要的改进:image-based, Ding etal. 3.对于样本挑选的改进: 1)hard samples: hard positive 和hard neg ...

  10. vue拓展题

    本文档基于vue-cli技术栈总结了 vue-cli工程 vue.js核心知识 vue-router路由 vuex状态管理器 axios等http请求 移动端适配 Tab切换等常用功能 vue与原生a ...