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");
 

最新文章

  1. 中国式商业智能报表ActiveReports免费公开课,10月20日开讲
  2. Python自动化之rabbitmq rpc client端代码分析(原创)
  3. nginx全局变量实例对照 rewrite参考手册
  4. 洛谷P2279 [HNOI2003]消防局的设立
  5. 初学structs2,表单验证简单补充
  6. 在 mongodb 终端环境下写多行 javascript 代码、函数
  7. spring中的aware接口
  8. Android平台调用WebService详解
  9. php实现MVC
  10. Angular 2.0 从0到1 (四)
  11. Fragment的使用简单介绍【Android】
  12. 使用Pig预测电信用户的移动路径
  13. Spring MVC 入门基础
  14. 两个栈实现队列+两个队列实现栈----java
  15. 关于oracle数据库备份还原-impdp,expdp
  16. SpringMVC 参数绑定注解解析
  17. Spring-shiro源码陶冶-DefaultFilter
  18. MYSQL IN 出现的慢查询问题
  19. Linux第十节课学习笔记
  20. js字符串转换成数字与数字转换成字符串的实现方法

热门文章

  1. nRF52832 串口仅支持偶检验
  2. Vue 自定义编译打包路径
  3. 各种系统性能优化技术,采用vilocity实现商品页面静态化
  4. 部分NLP工程师面试题总结
  5. c++ 字符串时间格式转换为时间 判断有效期
  6. php连接mysql8报错如何解决
  7. LeetCode:复原IP地址【93】
  8. kube-proxy运行机制分析【转载】
  9. 10 Spring框架--基于注解的IOC配置
  10. Java开发笔记(一百一十六)采用UDP协议的Socket通信