Spring Boot 获取ApplicationContext
package com.demo; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; @Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext = null; @Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
if (SpringUtils.applicationContext == null) {
SpringUtils.applicationContext = arg0;
}
} // 获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
} // 通过name获取 Bean.
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
} // 通过class获取Bean.
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
} // 通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
} }
与Spring Boot启动类同包或其子包
Spring Boot 获取ApplicationContext的更多相关文章
- Spring Boot获取前端页面参数的几种方式总结
Spring Boot的一个好处就是通过注解可以轻松获取前端页面的参数,之后可以将参数经过一系列处理传送到后台数据库. 获得的方式有很多种,这里稍微总结一下,大致分为以下几种: 1.指定前端url请求 ...
- Spring Boot 获取 java resources 下文件
Spring Boot 获取 java resources 下文件 Spring Boot 获取 resources 目录下的目录(例:获取 resources 目录下的 template 目录): ...
- Spring Spring boot 获取IOC中的bean,ApplicationContext
https://blog.csdn.net/weixin_38361347/article/details/89304414 https://www.jianshu.com/p/9ea13b00b1d ...
- Spring Boot - 获取所有的Bean信息
前言 Spring Boot启动的时候需要加载许多Bean实现最小化配置,本文将尝试找出Spring启动后加载的所有Bean信息: 通过ApplicationContext 去获取所有的Bean 通过 ...
- Spring Boot 获取Bean对象实体
一.实现 ApplicationContextAware 接口 package com.zxguan; import org.springframework.beans.BeansException; ...
- spring boot 的 ApplicationContext 及 getbean
在spring中,我们通过如下代码取得一个spring托管类: ApplicationContext ac = new FileSystemXmlApplicationContext("ap ...
- spring中获取applicationContext(2)
前几天写web项目的时候,用到了spring mvc. 但是又写bean.我要在代码里面生成,而这个bean里面,又有一些属性是通过spring注入的. 所以,只能通过ApplicationConte ...
- spring中获取applicationContext
常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXm ...
- Spring +quartz获取ApplicationContext上下文
job存在数据库中,能够进行动态的增增删改查,近期遇到了怎样获取ApplicationContext上下文的问题.解决的方法例如以下 applicationContext-quartz.xml < ...
随机推荐
- string 常用 方法
boost::array< char, 16 > header; string(header.begin(),header.end()) std::vector<uchar> ...
- Quartz动态配置表达的方法
在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度.有关调度的实现我就第一就想到了Quartz这个开源调度组件,因为很多项目使用过,Spring结合Quartz静态配置调度任务时间, ...
- 利用过采样技术提高ADC测量微弱信号时的分辨率
1. 引言 随着科学技术的发展,人们对宏观和微观世界逐步了解,越来越多领域(物理学.化学.天文学.军事雷达.地震学.生物医学等)的微弱信号需要被检测,例如:弱磁.弱光.微震动.小位移.心电.脑电等[1 ...
- 容斥原理——uva 10325 The Lottery
首先推荐一篇介绍容斥原理很好的博客http://www.cppblog.com/vici/archive/2011/09/05/155103.html 题意:求1~n中不能被给定m个数中任意一个数整除 ...
- codeforces 617B Chocolate
题意: 在给定01串中,问能分割成多少个子串?每个子串只有一个1. dp #include<iostream> #include<string> #include<alg ...
- AJAX— 异步传输
AJAX异步传输 AJAX— 异步传输 AJAX浏览器与服务器异步传输数据,通过异步请求大大减少数据信息访问量,通俗:AJAX对局部刷新而不是对整个页面刷新,AJAX使得程序更快,更友好. AJAX处 ...
- Code Understanding Step by Step - We Need a Task
Code understanding is a task we are always doing, though we are not even aware that we're doing it ...
- Android实例-设置消息提醒(XE8+小米2)
相关资料: 1.官网实例:http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Using_the_Notification ...
- 超级MINI STLINK V2 官方固件自动升级 ST-Link 【worldsing 笔记】
简介: 支持所有带SWIM接口的STM8系列单片机 支持所有带SWD接口的STM32系列单片机 完全兼容Keil,STVP,STVD,IAR,COSMIC,STM32 ST-LINK Utility! ...
- SQL中EXISTS的用法和效率
比如在Northwind数据库中有一个查询为SELECT c.CustomerId,CompanyName FROM Customers cWHERE EXISTS(SELECT OrderID FR ...