Spring ApplicationContext
2015-09-17 10:30
295 查看
In this tutorial we will know what is spring ApplicationContext and how to access it. There are numerous different ways to get a task done in Spring framework and that is one advantage of it.
What is
How to access
To get access to
The
In the case of spring web application we have a utility class provided by spring framework called
We can use this utility to get application context but we need to provide the respective servletcontext as parameter. Following source code demonstrates how we can get access to application context even from inside a simple plain java bean where we don’t have access to servlet context.
We will create the following utility class, it implements
This is a simple java bean which uses our utility method to get access to the
This example is based on a simple java application (not a web application). It uses
Lazy-instantiation and
By default spring implementations of
When the
What is ApplicationContext
?
ApplicationContextis an interface for providing configuration information to an application. There are multiple classes provided by springframework that implements this interface and helps us use configuration information in applications.
ApplicationContextprovides standard bean factory lifecycle capabilities. An important capability which we will be using in below code example is, class implementing
ApplicationContextshould scan for
ApplicationContextAwarebeans and invoke
setApplicationContextby passing an implementation of its instance.
ApplicationContext
vs BeanFactory
BeanFactoryis a subset of
ApplicaitonContextand provides lesser functionalities. When we need full capabilities with respect to configuration handling then we go for
ApplicationContext.
How to access ApplicationContext
inside a java bean?
To get access to ApplicationContextwe should implement
ApplicationContextAwareinterface in the respective java bean. It has a method,
void setApplicationContext(ApplicationContext applicationContext) throws BeansException
The
ApplicationContextimplementation which we are using in our application will invoke this method and pass the concrete object for
AppplicationContext. Using this we can get access to all the configuration information.
In the case of spring web application we have a utility class provided by spring framework called
WebApplicationContextUtils.
ServletContext servletContext = this.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
We can use this utility to get application context but we need to provide the respective servletcontext as parameter. Following source code demonstrates how we can get access to application context even from inside a simple plain java bean where we don’t have access to servlet context.
Sample to Access ApplicationContext
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="applicationContextUtils" class="com.javapapers.spring.ApplicationContextUtils"></bean> <bean id="helloWorld" class="com.javapapers.spring.HelloWorld" /> <bean id="strHelloWorld" class="java.lang.String"> <constructor-arg value="Hello World" /> </bean> </beans>
ApplicationContextUtils.java
We will create the following utility class, it implements ApplicationContextAwareand provides the
setApplicationContextwhich will be invoked by spring container and the
applicationContextwill be passed by it. We store it in a static variable and expose it through a get method so that it can be accessed all through the application.
package com.javapapers.spring; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class ApplicationContextUtils implements ApplicationContextAware { private static ApplicationContext ctx; @Override public void setApplicationContext(ApplicationContext appContext) throws BeansException { ctx = appContext; } public static ApplicationContext getApplicationContext() { return ctx; } }
HelloWorld.java
This is a simple java bean which uses our utility method to get access to the ApplicationContext.
package com.javapapers.spring; import org.springframework.context.ApplicationContext; public class HelloWorld { public String getValueFromContext(String beanName) { ApplicationContext appCtx = ApplicationContextUtils .getApplicationContext(); String strFromContext = (String) appCtx.getBean(beanName); return strFromContext; } }
TestAppContext.java
This example is based on a simple java application (not a web application). It uses ClassPathXmlApplicationContextwhich implements
ApplicationContext.
package com.javapapers.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAppContext { public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext( "META-INF/spring-context.xml"); HelloWorld hw = (HelloWorld) appContext.getBean("helloWorld"); String output = hw.getValueFromContext("strHelloWorld"); System.out.print(output); } }
Lazy-instantiation and ApplicationContext
By default spring implementations of ApplicationContexteagerly instantiate all the singleton beans at startup. This helps us to ensure all the configuration and dependencies are intact. This default behaviour can be customized as below by just adding property
lazy-init=”true”.
<bean id="lazy" class="com.javapapers.LazyBean" lazy-init="true"/>
When the
ApplicationContextis starting up this bean will not be instantiated and configured.
Annotation Based Access
Spring 2.5 onwards we can autowire theApplicationContextas well as
BeanFactoryas below,
private @Autowired ApplicationContext appContext; private @Autowired BeanFactory beanFactory;
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- 插件管理框架 for Delphi(一)
- 使用CSS框架布局的缺点和优点小结
- 列举PHP的Yii 2框架的开发优势
- Windows窗体的.Net框架绘图技术实现方法
- 浅谈JavaScript 框架分类
- 轻量级javascript 框架Backbone使用指南
- javascript实现框架高度随内容改变的方法
- JS刷新框架外页面七种实现代码
- 超赞的动手创建JavaScript框架的详细教程
- asp.net4.0框架下验证机制失效的原因及处理办法
- 插件管理框架 for Delphi(二)
- 零基础学习AJAX之AJAX框架
- Ajax 框架学习笔记
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- Flex中最好的MVC框架Mate框架
- JavaScript 异步调用框架 (Part 4 - 链式调用)