How to get spring application context object reference?
2015-09-17 17:26
633 查看
This page gives an example to get spring application context object with in non spring managed classes as well. It is not possible to have all classes as spring managed classes, in such classes you need to get spring application context object. This can be achieved by using
Create a new class and implement
Declate above bean in your
And finally here is the code to access application context and getting bean reference:
ApplicationContextAwareinterface. Here are the steps to achieve application context object:
Create a new class and implement
ApplicationContextAwaremethod and its unimplemented method as shown below:
package com.java2novice.spring; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class ApplicationContextProvider implements ApplicationContextAware{ private static ApplicationContext context; public ApplicationContext getApplicationContext() { return context; } @Override public void setApplicationContext(ApplicationContext ac) throws BeansException { context = ac; } }
Declate above bean in your
applicationContext.xmlfile as shown below:
<bean id="applicationContextProvder" class="com.java2novice.spring.ApplicationContextProvider"/>
And finally here is the code to access application context and getting bean reference:
ApplicationContextProvider appContext = new ApplicationContextProvider(); TestBean tb = appContext.getApplicationContext().getBean("testBean", TestBean.class);
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- java实现简单美女拼图游戏
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- 基于Spring框架的Shiro配置方法
- Spring MVC中上传文件实例
- 实例讲解Java的Spring框架中的AOP实现
- 解析Java中如何获取Spring中配置的bean
- Spring的注解配置与XML配置之间的比较
- java Spring整合Freemarker的详细步骤
- Java 二维码,QR码,J4L-QRCode 的资料整理
- java当中的定时器的4种使用方式
- java中 spring 定时任务 实现代码