手动获取spring的ApplicationContext和bean对象
WEB项目:
方法1:
1
|
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc) |
方法2:
1
|
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc) |
方法3:
1
|
写一个工具类类继承ApplicationObjectSupport,并将这个加入到spring的容器 |
方法4:
1
|
写一个工具类类继承WebApplicationObjectSupport,并将这个加入到spring的容器 |
方法5:(推荐)
1
|
写一个工具类实现ApplicationContextAware接口,并将这个加入到spring的容器 |
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import java.util.Map; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * 获取ApplicationContext和Object的工具类 * @author yzl * */ @SuppressWarnings ({ "rawtypes" , "unchecked" }) public class SpringContextUtils implements ApplicationContextAware { private static ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext arg0) throws BeansException { applicationContext = arg0; } /** * 获取applicationContext对象 * @return */ public static ApplicationContext getApplicationContext(){ return applicationContext; } /** * 根据bean的id来查找对象 * @param id * @return */ public static Object getBeanById(String id){ return applicationContext.getBean(id); } /** * 根据bean的class来查找对象 * @param c * @return */ public static Object getBeanByClass(Class c){ return applicationContext.getBean(c); } /** * 根据bean的class来查找所有的对象(包括子类) * @param c * @return */ public static Map getBeansByClass(Class c){ return applicationContext.getBeansOfType(c); } } |
非WEB项目
1
|
ApplicationContext ac = new FileSystemXmlApplicationContext( "applicationContext.xml" ) |
可选的操作方法有:
1
2
3
4
5
6
7
8
9
10
11
|
一: String[] path={ "WebRoot/WEB-INF/applicationContext.xml" , "WebRoot/WEB-INF/applicationContext_task.xml" }; ApplicationContext context = new FileSystemXmlApplicationContext(path); 二: String path= "WebRoot/WEB-INF/applicationContext*.xml" ; ApplicationContext context = new FileSystemXmlApplicationContext(path); 三: ApplicationContext ctx = new FileSystemXmlApplicationContext( "classpath:地址" ); 没有classpath的话就是从当前的工作目录 |
另外关于对于接口ApplicationContextAware的理解,可以参考转载的另一篇笔记,深入浅出写的不错:
org.springframework.context.ApplicationContextAware使用理解
转自:http://www.cnblogs.com/yangzhilong/p/3949332.html
手动获取spring的ApplicationContext和bean对象的更多相关文章
- 如何手动获取Spring容器中的bean(ApplicationContextAware 接口)
ApplicationContextAware 接口的作用 先来看下Spring API 中对于 ApplicationContextAware 这个接口的描述: 即是说,当一个类实现了这个接口之 ...
- 获取spring的ApplicationContext几种方式【转】
转自:http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html Java类获取spring 容器的bean 常用的5种获取spring 中bean的方式 ...
- SpringBoot 之 普通类获取Spring容器中的bean
[十]SpringBoot 之 普通类获取Spring容器中的bean 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...
- 获取Spring容器中的Bean协助调试
在使用Spring进行开发时,有时调bug真的是很伤脑筋的一件事,我们可以通过自定义一个监听器来获取Spring容器中的Bean实例来协助我们调试. 第一步:编写自定义监听器 /** * 监听serv ...
- 【Spring】手动获取spring容器对象时,报no qualifying bean of type is defined
手动获取容器对象时,报no qualifying bean of type is defined, 经过调查,发现手动获取的时候,该类所在的包必须经过spring容器初始化. 1.SpringConf ...
- 拦截器通过Spring获取工厂类,注入bean对象
// 这里需要注意一点,我们在拦截器内无法通过SpringBean的方式注入LoggerJPA,我只能通过另外一种形式. /** * 根据传入的类型获取spring管理的对应dao * @param ...
- 获取Spring容器中的Bean
摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...
- 怎么获取Spring的ApplicationContext
在 WEB 开发中,可能会非常少须要显示的获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean, 今天我就遇到了,在这里和大家分享一下, WEB 开发中,怎么获 ...
- [十]SpringBoot 之 普通类获取Spring容器中的bean
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...
随机推荐
- Ubuntu 14 安装 .Net Core
.Net Core的安装包的分发地址如下: https://apt-mo.trafficmanager.net/repos/dotnet-release/pool/main/d/ 方法一: 可以分别手 ...
- C# Azure 存储-分布式缓存Redis工具类 RedisHelper
using System; using System.Collections.Generic; using Newtonsoft.Json; using StackExchange.Redis; na ...
- PHP 数据访问
如何连接 1.造连接对象 $db= new MySQLi("localhost","root","123","mydb" ...
- int and string
int转string一.#include <sstream> int n = 0; std::stringstream ss; std::string str; ss<<n; ...
- SpringMVC配置项学习笔记
1. <mvc:annotation-driven /> <mvc:annotation-driven />是一种简写形式,默认会注册DefaultAnnotationHand ...
- Android开发-之SQLite数据库
之前我们讲了如何将数据存储在文件中,那么除了这种方式呢,就是我们常见的大家都知道的将数据存储在数据库当中了. 将数据存储在数据库中的优势: 1)存储在数据库中的数据更加方便操作,比如增.删.改.查等 ...
- Publication的 immediate_sync 属性
Publication的属性 immediate_sync 控制 Snapshot 文件的创建,如果属性 immediate_sync设置为true,那么snapshot file在snapshot ...
- 小谈MVC 模式
MVC (Modal View Controler) 本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使 ...
- OpenCascade Eigenvalues and Eigenvectors of Square Matrix
OpenCascade Eigenvalues and Eigenvectors of Square Matrix eryar@163.com Abstract. OpenCascade use th ...
- 爆一个VS2015 Update1更新带来的编译BUG【已有解决方案】
一个编译的BUG,在原生的VS2015中没有问题,但更新至VS2015 Update1之后就有了,大概是Update1用了新的编译器,害得我好苦.经测试,VS2013 Update5中也存在这个问题, ...