Spring中ApplicationContextAware的使用方法详解
作者:霞光里 发布时间:2023-12-25 07:01:33
标签:Spring,ApplicationContextAware
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
0
投稿
猜你喜欢
- 效果:代码:XmlDocument xml = new XmlDocument(); &nbs
- 对象类型转换分为向上转型和向下转型(强制对象转型)。 向上转型是子对象向父对象转型的过程,例如猫类转换为动物类;向下转型是强制转型实现的,是
- 本文实例为大家分享了android Matrix图片随意放大缩小和拖动的具体代码,供大家参考,具体内容如下step1:新建一个项目DragA
- 1. 我们可以通过将字符强转为int型进行输出那么在控制台中我们将会得到字符的ascii值,这里我们使用nextLine()方法来接收字符串
- Spring整合mybatis的mapper生成过程mapperScannerConfigurer实现了BeandifinitionRegi
- 利用反射获取对象的所有属性及对应的值1、获取属性名数组private static String[] getFiledName(Object
- 这篇文章主要介绍了java操作elasticsearch的案例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价
- 1.user实体package com.demo.dto;public class User { private Integer
- 委托定义如下:public class SocketSp{ public delegate void ReceiveComplet
- 一、 代码块的概念在探究对象初始化顺序之前,我们先通过代码来了解一下代码块的概念。class Test{ public stat
- 在开发中,遇到了sql语句报错,但是并没有回滚的情况。经过几天的排查,终于找到了事务没有回滚的原因。原来的项目用的是informix的数据库
- 1.super介绍我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。用于访问父类的属性,方法,构造器2.super
- 本文实例为大家分享了java实现图片分割指定大小的具体代码,供大家参考,具体内容如下1.使用工具:ThumbnailsThumbnails
- Java7引入了Fork Join的概念,来更好的支持并行运算。顾名思义,Fork Join类似与流程语言的分支,合并的概念。也就是说Jav
- Grpc是googe开发的,是一款语言中立、平台中立、开源的远程过程调用(RPC)系统。新公司的项目服务之间的调用使用的Grpc来实现服务间
- 当前使用的IDEA版本是2020.1。随着IDEA版本的升级,有些插件不再支持,而有些插件变成了收费插件,这些插件将不再推荐。以下列举的,都
- 需要实现看门狗功能,定时检测另外一个程序是否在运行,使用 crontab 仅可以实现检测程序是否正在运行,无法做到扩展,如:手动重启、程序升
- 摘要在生产环境下,我们需要关闭swagger配置,避免暴露接口的这种危险行为。方法禁用方法1:使用注解 @Value() 推荐使用packa
- 一、前言二、案例需求1.编写login.html登录页面,username&password两个输入框2.使用Druid数据库连接池
- 我们的spring cloud微服务一般是打成jar包发布的,Linux下启动jar包和windows下一样,都是java -jar 包名,