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查询的扩展需要用字符串拼接的方式进行,这往往会导致代码的阅读困难:通过字符串对域类型和属性的不安 ...
随机推荐
- Android新增的注解
环境 使用Android注解前需要导入相关的包 compile 'com.android.support:support-annotations:latest.integration' 注意:如果我们 ...
- C99一些特性
__FILE__ 对应代码文件名__LINE__ 对应代码行号__DATE____TIME____FUNC__ __FUNCTION__ 在Visual Studio 2005中,默认情况下, ...
- hdu2579之BFS
Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- linux fdisk分区工具
fdisk来自IBM老牌分区工具,支持绝大多数操作系统,几乎所有的linux的发行版本都装有disk,包括linux的resure模式下的依然能够使用. fdisk是一个机遇MBR的分区工具,所有如果 ...
- C# xsd 验证 XML数据有效性 问题
使用XSD进行批量数据导入时生成的XML数据有效性这样的功能已经不是第一次做了,之前做的时候都没有碰到什么问题,这些天在开发中遇到了一个很头痛的问题就是无论XSD文件规则怎么写,验证都是通过的. 下面 ...
- [LeetCode 题解]: palindromes
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- Android View与ViewGroup 关系
View派生出的直接子类有:AnalogClock,ImageView,KeyboardView, ProgressBar,SurfaceView,TextView,ViewGroup,ViewStu ...
- 在Linux下启动Java服务的脚本
#!/bin/sh #该脚本为Linux下启动java程序的通用脚本.即可以作为开机自启动service脚本被调用, #也可以作为启动java程序的独立脚本来使用. # #Author: tudaxi ...
- AJAX方式调用百度天气
后台代码: [HttpPost] public string AjaxWeather() { string CityName = string.IsNullOrEmpty(Request.Form[& ...
- django中models联合唯一unique_together
例: 文章点赞 class ArticleUpDown(models.Model): """ 点赞表 """ nid = models.Au ...