SpringBoot项目中获取applicationContext对象
ApplicationContext 对象是Spring开源框架的上下文对象实例,也就是我们常说的Spring容器,一般情况下我们是不用手动来管理它,而是由Spring框架自己来维护bean之间的关系,但不排除我们有的时候需要手动获取Spring容器(比如获取多例bean的时候),传统的获取ApplicationContext 的方式有很多种,这里介绍官方推荐的方式:使用ApplicationContextAware接口。
其实以实现ApplicationContextAware接口的方式获取ApplicationContext 对象实例并不是SpringBoot特有的功能,早在Spring3.0x版本之后就存在了这个接口,在传统的Spring项目内同样是可以以实现ApplicationContextAware接口的方式获取到ApplicationContext 实例的。具体使用代码如下:
package com.example.demo; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; @Component
public class MyApplicationContext implements ApplicationContextAware {
/**
* 上下文对象实例
*/
private ApplicationContext applicationContext; /**
* 实现ApplicationContextAware接口的方法
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} /**
* 获取applicationContext
*
* @return
*/
public ApplicationContext getApplicationContext() {
return applicationContext;
} /**
* 通过bean的名字获取 Bean.
*
*/
public Object getBean(String name) {
return getApplicationContext().getBean(name);
} /**
* 通过类名获取Bean.
*
*/
public <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
} }
在MyApplicationContext上是有@Component注解的,Spring会自动调用setApplicationContext方法来为我们设置上下文实例,所以我们可以在需要获取上下文的实体内中注入MyApplicationContext对象,进而使用其中的方法来获取上下文对象等。如果想在非Spring管理的实体内使用ApplicationContext,这时我们可以修改ApplicationContext实例对象为静态实例,方法改为静态方法,这样在外部同样是可以获取到指定Bean的实例,示例如下:
package com.example.demo; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; @Component
public class MyApplicationContextUtil implements ApplicationContextAware {
/**
* 上下文对象实例
*/
private static ApplicationContext myApplicationContext; /**
* 实现ApplicationContextAware接口的方法
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
myApplicationContext = applicationContext;
} /**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return myApplicationContext;
} /**
* 通过bean的名字获取 Bean.
*
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
} /**
* 通过类名获取Bean.
*
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
}
SpringBoot项目中获取applicationContext对象的更多相关文章
- springboot 项目中获取默认注入的序列化对象 ObjectMapper
在 springboot 项目中使用 @SpringBootApplication 会自动标记 @EnableAutoConfiguration 在接口中经常需要使用时间类型,Date ,如果想要格式 ...
- spring中获取ApplicationContext对象的技巧,含源码说明
第一步,实现接口ApplicationContextAware,重写setApplicationContext方法,下方代码标红的地方,绿色部分 可以通过声明来进行存储到本类中. @Component ...
- 在web项目中获取ApplicationContext上下文的3种主要方式及适用情况
最近在做web项目,需要写一些工具方法,涉及到通过Java代码来获取spring中配置的bean,并对该bean进行操作的情形.而最关键的一步就是获取ApplicationContext,过程中纠结和 ...
- 在springboot项目中获取pom.xml中的信息
最近做了一个新项目,用到了springboot.在搭建框架的过程中,需要读取pom.xml中version的值,本来想着是用自己用java解析xml来着.没想到maven提供了这么一个包,可以直接获取 ...
- linux环境下在springboot项目中获取项目路径(用于保存文件等)
//application.properties中设置:(file.path=static/qrfile/)//保存到static文件夹下的qrfile目录@Value("${file.pa ...
- web项目中获取spring的bean对象
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...
- 在Spring应用中创建全局获取ApplicationContext对象
在Spring应用中创建全局获取ApplicationContext对象 1.需要创建一个类,实现接口ApplicationContextAware的setApplicationContext方法. ...
- SpringBoot项目中遇到的BUG
1.启动项目的时候报错 1.Error starting ApplicationContext. To display the auto-configuration report re-run you ...
- SpringBoot12 QueryDSL01之QueryDSL介绍、springBoot项目中集成QueryDSL
1 QueryDSL介绍 1.1 背景 QueryDSL的诞生解决了HQL查询类型安全方面的缺陷:HQL查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...
随机推荐
- xfce4快捷键设置
xfce4的"Keyboard"可以方便的设置启动应用程序的快捷键. 例如,添加xfce4-terminal和emacs的启动快捷键 Alt+F3打开"Applicati ...
- Finding Memory Leaks with SAP Memory Analyzer
Introduction There is a common understanding that a single snapshot of the java heap is not enough f ...
- 试题 B: 不同子串 蓝桥杯
[问题描述]一个字符串的非空子串是指字符串中长度至少为 1 的连续的一段字符组成的串.例如,字符串aaab 有非空子串a, b, aa, ab, aaa, aab, aaab,一共 7 个.注意在计算 ...
- Android-广播概念
Android中的消息机制 1.Handler+Message消息机制,是用于子线程与主线程的通讯: 2.广播+广播接收者也是消息机制,是重量级别的,四大组件之一,需要激活组件,是用于组件和组件之间通 ...
- 一些参考网站 - Reference Documentation - Website Address
Reference Documentation - Website Address MSDN Visual Studio 2015官方文档 https://msdn.microsoft.com/zh- ...
- CentOS下Docker与.netcore(一) 之 安装
CentOS下Docker与.netcore(一) 之 安装 CentOS下Docker与.netcore(二) 之 Dockerfile CentOS下Docker与.netcore(三)之 三剑客 ...
- Zeal - 开源离线开发文档浏览器
https://zealdocs.org/ win10上暂时安装版会crash,请用portalable的解压版
- django view 装饰器
Django提供了几个可以应用于视图以支持各种HTTP特性的装饰器 Allowed HTTP django.views.decorators.http里的装饰器可以根据请求方法限制对视图的访问. re ...
- mvn install 报错Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2 错误: 找不到符号
报错信息 在Eclipse中执行mvn install时,console端显示如下报错信息: [ERROR] Failed to execute goal org.apache.maven.plugi ...
- nowcoder(牛客网)提高组模拟赛第一场 解题报告
T1 中位数(二分) 这个题是一个二分(听说是上周atcoder beginner contest的D题???) 我们可以开一个数组b存a,sort然后二分b进行check(从后往前直接遍历check ...