How to get spring application context object reference? - See more at: http://www.java2novice.com/sp
来源:互联网 发布:淘宝店资质高说明什么 编辑:程序博客网 时间:2024/06/05 11:25
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 ·ApplicationContextAware· interface. Here are the steps to achieve application context object:
Create a new class and implement ApplicationContextAware
method 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.xml
file 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();//肯定不是自己new出来的,肯定是从ApplicationContext中获取的TestBean tb = appContext.getApplicationContext().getBean("testBean", TestBean.class);
0 0
- How to get spring application context object reference? - See more at: http://www.java2novice.com/sp
- How to get spring application context object reference?
- How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/
- How to change the price range in layered navigation in Magento? - See more at: http://www.expertweba
- When to use Tomcat CATALINA_OPTS instead of JAVA_OPTS - See more at: http://www.tikalk.com/java/when
- Spring AOP Advices - Before advice example - xml based configuration - See more at: http://www.java2
- Wireshark – No interfaces to capture on using Ubuntu 12.04 - See more at: http://www.networkingnut.n
- http://www.mkyong.com/maven/how-to-create-a-web-application-project-with-maven/
- Intelligent Applications: The Big Data Theme for 2013 - See more at: http://blog.infochimps.com/2013
- How to get the visible region of a window or CDC(转自http://www.programmersheaven.com)
- Almost everything you need to know to enter mobile game development on 2013 - See more at: http://ww
- svn: Can't open file 'PATH/TO/YOUR/FILES/.svn/lock': Permission denied - See more at: http://ericlon
- <stdexcept> http://www.cplusplus.com/reference/stdexcept/
- How To: Download a large, custom set of records from NCBI (pls see http://www.ncbi.nlm.nih.gov/guide
- Get more...go to my another blog...http://blog.sina.com.cn/qqlove163
- how to get callstack at runtime?
- http://www.killdb.com/2012/12/06/how-to-extract-datafiles-from-asm-diskgroup.html
- Unable to locate Spring NamespaceHandler for XML schema namespace 【http://www.springframework.org/schema/context】
- 如何在xcode6以及以上版本中添加.pch文件
- https 简单介绍
- MySQL Study之--MySQL的InnoDB索引原理详解
- SpringMVC入门
- 组合数中的Lucas定理
- How to get spring application context object reference? - See more at: http://www.java2novice.com/sp
- ActionBar关于overflow三个点菜单和search搜索按钮的显示问题
- iOS支付[支付宝、银联、微信]
- ARPG客户端战斗架构设计
- unity3d 制造自己的水体water effect
- 在web page中嵌入ace editor
- css3 box-sizing属性(规定尺寸)
- 几种防止表单重复提交的方法
- HDU 1207 汉诺塔II(递推)