Spring中ApplicationContextAware接口的用法
1.为什么使用AppplicationContextAware?
ApplicationContext的BeanFactory 的子类, 拥有更强大的功能,ApplicationContext可以在服务器启动的时候自动实例化所有的bean,而 BeanFactory只有在调用getBean()的时候才去实例化那个bean, 这也是我们为什么要得到一个ApplicationContext对象, 事实上Spring2相关的web应用默认使用的是ApplicationContext对象去实例化bean, 换一句话说, 在服务器启动的时候,Spring容器就已经实例化好了一个ApplicationContext对象,所以我们要在老的代码里尝试去获取这个对象。 但是如何才能得到一个ApplicationContext对象呢?方法很多,最常用的办法就是用ClassPathXmlApplicationContext, FileSystemClassPathXmlApplicationContext, FileSystemXmlApplicationContext 等对象去加载Spring配置文件,这样做也是可以, 但是在加载Spring配置文件的时候,就会生成一个新的ApplicaitonContext对象而不是Spring容器帮我们生成的哪一个, 这样就产生了冗余, 所以我们在这里不采用这种加载文件的方式,我们使用ApplicationContextAware让Spring容器传递自己生成的ApplicationContext给我们, 然后我们把这个ApplicationContext设置成一个类的静态变量, 这样我们就随时都可以在老的代码里得到Application的对象了。(转载自 https://blog.csdn.net/kouwoo/article/details/43405109)
2. ApplicationContextAware接口作用 ?
加载Spring配置文件时,如果Spring配置文件中所定义或者注解自动注入的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的方法:
public void setApplicationContext (ApplicationContext context) throws BeansException
3.如何实现的
首先创建工具类实现ApplicationContextAware
public class GetBeanInstance implements ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger(GetBeanInstance.class);
private static ApplicationContext applicationContext = null;
/***
* 当继承了ApplicationContextAware类之后,那么程序在调用 getBean(String)的时候会自动调用该方法,不用自己操作
*/
public void setApplicationContext(ApplicationContext applicationContext) {
logger.info("setApplicationContext :::: " + applicationContext);
GetBeanInstance.applicationContext = applicationContext;
}
// 获取 bean
public static Object getBean(String beanName) {
try {
if(applicationContext == null){
logger.error("applicationContext is null");
}
return applicationContext.getBean(beanName);
} catch (Exception e) {
logger.warn("not fund bean [" + beanName + "]", e);
return null;
}
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String beanName, Class<T> clazz) {
return (T) getBean(beanName);
}
public static Object getBeanThrowException(String beanID) {
return getBeanThrowException(beanID, Object.class);
}
@SuppressWarnings("unchecked")
public static <T> T getBeanThrowException(String beanID, Class<T> clazz) {
if (beanID == null || "".equals(beanID)) {
throw new IllegalArgumentException("beanID is empty [" + beanID + "]");
}
try {
return (T) applicationContext.getBean(beanID);
} catch (Exception e) {
logger.error("not fund bean [" + beanID + "]", e);
throw new NullPointerException("not fund bean [" + beanID + "] !!!!!!!");
}
}
}
然后配置 GetBeanInstance
<bean id="GetBeanInstancesAPI" class="com.sinosoft.utility.GetBeanInstance" scope="singleton" lazy-init="false" />
最后配置web.xml
因为spring要建立属于自己的容器,就必须要加载自己的配置文件。 这个时候,需要注册ContextLoaderListener或者这个类的子类。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
当然,这样子的话只会读取默认路径下的application.xml配置文件的。如果需要读取特定路径下的配置文件。需要在web.xml中添加如下信息。
<context-param>
<param-name>contextConfigLocation</param-name> //这行不允许改动
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
到这就就大功告成了。虽然还是迷迷糊糊,但总算是了解了一些知识。
Spring中ApplicationContextAware接口的用法的更多相关文章
- spring中ApplicationContextAware接口描述
项目中使用了大量的工厂类,采用了简单工厂模式: 通过该工厂类可以获取指定的处理器bean,这些处理器bean我们是从spring容器中获取的,如何获取,是通过实现ApplicationContextA ...
- spring中ApplicationContextAware接口使用理解
一.这个接口有什么用?当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean.换句话说,就是这个类可以直 ...
- Spring中ApplicationContextAware接口的说明
转载 https://www.cnblogs.com/muqingzhi123/p/9805623.html 1.为什么使用AppplicationContextAware? ApplicationC ...
- Spring中JdbcTemplate的基础用法
Spring中JdbcTemplate的基础用法 1.在DAO中使用JdbcTemplate 一般都是在DAO类中使用JdbcTimplate,在XML配置文件中配置好后,可以在DAO中注入即可. 在 ...
- Spring中Ordered接口简介
目录 前言 Ordered接口介绍 Ordered接口在Spring中的使用 总结 前言 Spring中提供了一个Ordered接口.Ordered接口,顾名思义,就是用来排序的. Spring是一个 ...
- spring 中配置sessionFactory及用法
spring 中配置sessionFactory及用法 方法一: 1.在Spring的applicationContext.xml中配置bean <!-- 启用注解注入 --> ...
- 转:spring中InitailizingBean接口的简单理解
转自:https://www.cnblogs.com/wxgblogs/p/6849782.html spring中InitializingBean接口使用理解 InitializingBean接 ...
- 全面解析Spring中@ModelAttribute注解的用法
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...
- Spring中ApplicationContextAware的用法
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt379 一.这个接口有什么用? 当一个类实现了这个接口(Application ...
随机推荐
- [GKCTF2020]CheckIN 注意了解多方面的东西
打开之后是这样的,没有发现反序列化函数,但是发现有一个@eval,想到了一句话,这是用base64进行传参首先传参phpinfo();看看,需要经过base64编码 http://e0cc90ac-d ...
- John the Ripper快速密码破解工具简单使用
在某场比赛中师傅们说需要用到该工具,学习之 题目给了我们一个流量包,分析 发现 .hint.php.swp文件 该文件是使用 vim 编辑文件时异常退出而产生的,可以通过 vim -r 文件名 进行相 ...
- (转)MySQL优化原理
原文:https://mp.weixin.qq.com/s__biz=MzI4NTA1MDEwNg==&mid=2650763421&idx=1&sn=2515421f09c1 ...
- 对象存储COS全球加速助力企业出海
近年来,中国互联网行业迅猛发展,国内庞大的市场孕育出了许多现象级的产品,也锤炼出了非常成熟的产业链.与此同时,很多海外市场还处于萌芽期,存在着巨大的流量红利,越来越多的互联网企业开始加速"出 ...
- Android Studio中SVN的使用
1.忽略文件 1)这种方式,每次新建一个项目都要添加,并不是全局的. .idea文件夹 .gradle文件夹 所有的build文件夹 所有的.iml文件 local.properties文件 2)使用 ...
- 通过Spring profile方式实现多环境部署
1 多环境部署 在实际软件开发和部署过程中,我们的软件往往需要在不同的运行环境中运行.例如,各个环境数据库地址不同,需要单独配置.spring高级装备中提供profile,来支持多环境部署. 1.1 ...
- Electron安装打包指南
当前环境Debian Linux-Deepin 安装Node 直接下载 命令下载 下载 wget https://nodejs.org/dist/v14.15.1/node-v14.15.1-linu ...
- Linux下设置定时任务删除归档日志
1.编辑删除归档日志的脚本----/home/oracle/clearlog.sh #! /bin/bash source ~/profile_orcl#记录归档删除的日志exec >> ...
- Java基础数据类型详解
在Java中的数据类型一共有8种,大致分为整型(4个)浮点型(2个)布尔(1)字符(1个) 分类 类型 默认值 占用字节 范围 整型 byte 0 1 = 8 bit -2^7 - 2^7 short ...
- Spark-5-如何定位导致数据倾斜的代码
数据倾斜只会发生在shuffle过程中.这里给大家罗列一些常用的并且可能会触发shuffle操作的算子:distinct.groupByKey.reduceByKey.aggregateByKey.j ...