spring在非容器管理的类里获取bean
package com.qmtt.tools; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* @author wujf
*
*/
@Component
public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
} // 获取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在非容器管理的类里获取bean的更多相关文章
- VS2010 MFC中 在FormView派生类里获取文档类指针的方法
经过苦苦调试,今晚终于解决了一个大问题. 我想要实现的是:在一个FormView的派生类里获取到文档类的指针. 但是出现问题:试了很多办法,始终无法获取到. 终于,此问题在我不懈地调试加尝试下解决了. ...
- 在dwr的调用类里获取请求信息
在dwr的调用类里获取请求的相关信息HttpSession session = WebContextFactory.get().getSession();HttpServletResponse res ...
- Spring源码剖析4:其余方式获取Bean的过程分析
原型Bean加载过程 之前的文章,分析了非懒加载的单例Bean整个加载过程,除了非懒加载的单例Bean之外,Spring中还有一种Bean就是原型(Prototype)的Bean,看一下定义方式: 1 ...
- 【Spring】初始化Spring IoC容器(非Web应用),并获取Bean
参考文章 Introduction to the Spring IoC container and beans BeanFactory 和ApplicationContext(Bean工厂和应用上下文 ...
- Spring源码解析 – @Configuration配置类及注解Bean的解析
在分析Spring 容器创建过程时,我们知道容器默认会加载一些后置处理器PostPRocessor,以AnnotationConfigApplicationContext为例,在构造函数中初始化rea ...
- Spring中使用两种Aware接口自定义获取bean
在使用spring编程时,常常会遇到想根据bean的名称来获取相应的bean对象,这时候,就可以通过实现BeanFactoryAware来满足需求,代码很简单: @Servicepublic clas ...
- 在java中如何在非servlet的普通类中获取request、response、session
原文:http://blog.csdn.net/u012255097/article/details/53092628 在spring的普通类中: HttpServletRequest request ...
- Spring Boot Service注入为null mapper注入为null @Component注解下@Value获取不到值 WebsocketServer类里无法注入service
最近搞了一下websocket前台(这个网上有很多的教程这里就不班门弄斧啦) 以及前后台的交互 和后台的bug(搞了两天) 也是状态频发 bug不断 下面说一说问题. Websocket主类里面无法注 ...
- 在普通Java类里使用spring里注入的service、dao等
版权声明:本文为博主武伟峰原创文章,转载请注明地址http://blog.csdn.net/tianyaleixiaowu. 在spring管理的web项目里,譬如Struts和spring的项目,配 ...
随机推荐
- Mac JDK 多版本共存
1. 安装各JDK版本,安装后通过Java -version检测是否安装好 2. 打开~/.bash_profile,没有的话创建 vim ~/.bash_profile ...
- MySQL的IFNULL简单使用说明
MySQL IFNULL函数简介 MySQL IFNULL函数是MySQL控制流函数之一,它接受两个参数,如果不是NULL,则返回第一个参数. 否则,IFNULL函数返回第二个参数. 两个参数可以是文 ...
- poj 2771 Guardian of Decency(最大独立数)
题意:人与人之间满足4个条件之一即不能成为一对(也就说这4个条件都不满足才能成为一对),求可能的最多的单身人数. 思路:把男女分为两部分,接下来就是二分图的匹配问题.把能成为一对的之间连边,然后求出最 ...
- java中io类型及成熟io框架
就io本身而言,概念上有5中模型:blocking I/O, nonblocking I/O, I/O multiplexing(select and poll), singal driven I/O ...
- bzoj-1012 1012: [JSOI2008]最大数maxnumber(线段树)
题目链接: 1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MB Description 现在请求你维护一个数列,要 ...
- 关于tensorflow中维度的问题
一直对TF中tensor的reduce操作涉及的axis(reduction_indices)计算一知半解,这里系统总结一下,避免继续走弯路: 1.本质上来说,reduce_xxx都是降维操作,沿某个 ...
- C++实现利用(前序和中序生成二叉树)以及(二叉树的镜像)
#include<iostream> #include<string.h> #include<stack> using namespace std; typedef ...
- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解
转自:https://www.cnblogs.com/lmei/p/7190755.html?utm_source=itdadao&utm_medium=referral @_@ 写在最前 之 ...
- 设计模式-策略模式---Strategy(对象行为型)
策略模式 1.概念 分别封装起来,让他们之间可以相互替换,此模式让算法的变化独立于使用算法的客户. 2.代码实现:(模拟鸭子应用)https://git.oschina.net/ipnunu/Desi ...
- J20170507-ts
プロンプト n. 提示 オブジェクト n. 对象 アスタリスク * アンパサンド & スラッシュ / イテレータ n 迭代器 差し詰め 当前 スペル spell ...