Spring中ApplicationContextAware的使用方法详解
作者:霞光里 时间:2023-12-25 07:01:33
ApplicationContextAware
通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware
接口中的setApplicationContext
方法。
我们在ApplicationContextAware
的实现类中,就可以通过这个上下文环境对象得到Spring容器中的Bean。
看到—Aware就知道是干什么的了,就是属性注入的,但是这个ApplicationContextAware的不同地方在于,实现了这个接口的bean,当spring容器初始化的时候,会自动的将ApplicationContext注入进来:
使用方法如下:
1.实现ApplicationContextAware接口:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.co.ayz.rpc.registry.ServiceRegistry;
public class RpcServer implements ApplicationContextAware{
private ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
// TODO Auto-generated method stub
context = applicationContext;
}
//获得applicationContext
public static ApplicationContext getApplicationContext() {
//assertContextInjected();
return context;
}
public static void clearHolder(){
context=null;
}
//获取Bean
public static <T> T getBean(Class<T> requiredType){
//assertContextInjected();
return (T) getApplicationContext().getBean(requiredType);
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name){
assertContextInjected();
return (T) getApplicationContext().getBean(name);
}
//判断application是否为空
public static void assertContextInjected(){
Validate.isTrue(context==null, "application未注入 ,请在springContext.xml中注入SpringHolder!");
}
}
因为我们在做开发的时候,并不是说在每一个地方都能将属性注入到我们想要的地方去的,比如在Utils使用到dao,我们就不能直接注入了,这个时候就是我们需要封装springContext的时候了,而ApplicationContextAware就起了关键性的作用。
自己写的demo:
/**
* @Title: SpringJobBeanFactory.java
* @Package com.founder.mrp.job
* @Description: TODO
* @version V1.0
*/
package com.founder.mrp.job;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringJobBeanFactory implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringJobBeanFactory.applicationContext=applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException {
if (applicationContext == null){
return null;
}
return (T)applicationContext.getBean(name);
}
}
使用:
TypeSetErpService typeSetErpServ = SpringJobBeanFactory.getBean("typeSetErpServiceImpl");
来源:https://www.cnblogs.com/loong-hon/p/10917755.html
标签:Spring,ApplicationContextAware
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现的各种排序算法(插入排序、选择排序算法、冒泡排序算法)
2023-06-29 11:25:28
Mybatis中如何进行批量更新(updateBatch)
2022-10-11 13:42:00
Android实现IM多人员组合的群组头像
2023-05-24 06:39:54
![](https://img.aspxhome.com/file/2023/9/122229_0s.jpg)
C#自定义类型强制转换实例分析
2022-01-14 08:20:06
Java SSM实现前后端协议联调详解上篇
2023-09-16 20:42:22
![](https://img.aspxhome.com/file/2023/2/125322_0s.png)
SpringBoot整合TKMyBatis实现单表增删改查操作
2022-01-30 19:52:28
![](https://img.aspxhome.com/file/2023/2/82042_0s.png)
mybatis-plus 拦截器敏感字段加解密的实现
2023-12-20 05:58:17
通过spring boot 设置tomcat解决 post参数限制问题
2022-09-26 23:38:31
C# 对XML基本操作代码总结
2022-08-21 16:09:09
Android中Bitmap用法实例分析
2023-03-21 11:06:31
mybatis @Alias注解在类上的使用方式(推荐)
2023-11-20 00:30:03
控制Android LED灯颜色的代码实例
2022-12-30 02:53:20
Android组合控件自定义标题栏
2021-11-04 01:12:36
Spring生命周期回调与容器扩展详解
2023-04-05 16:26:31
C#byte数组与Image的相互转换实例代码
2023-08-15 16:15:51
java图片格式转换的三段代码
2023-01-22 05:43:02
Android开发之绘制平面上的多边形功能分析
2023-12-13 13:31:57
C# Volatile的具体使用
2023-11-21 11:14:50
![](https://img.aspxhome.com/file/2023/0/71740_0s.png)
spring boot使用thymeleaf为模板的基本步骤介绍
2023-12-13 15:07:23
![](https://img.aspxhome.com/file/2023/9/98699_0s.png)
Java8 CompletableFuture详解
2023-09-16 12:34:54