您的位置:首页 > 移动开发

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
ApplicationContext
?

ApplicationContext
is 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.
ApplicationContext
provides standard bean factory lifecycle capabilities. An important capability which we will be using in below code example is, class implementing
ApplicationContext
should scan for
ApplicationContextAware
beans and invoke
setApplicationContext
by passing an implementation of its instance.

ApplicationContext
vs
BeanFactory

BeanFactory
is a subset of
ApplicaitonContext
and 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
ApplicationContext
we should implement
ApplicationContextAware
interface in the respective java bean. It has a method,

void setApplicationContext(ApplicationContext applicationContext)
throws BeansException


The
ApplicationContext
implementation 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
ApplicationContextAware
and provides the
setApplicationContext
which will be invoked by spring container and the
applicationContext
will 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
ClassPathXmlApplicationContext
which 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
ApplicationContext
eagerly 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
ApplicationContext
is starting up this bean will not be instantiated and configured.

Annotation Based Access

Spring 2.5 onwards we can autowire the
ApplicationContext
as well as
BeanFactory
as below,

private @Autowired ApplicationContext appContext;
private @Autowired BeanFactory beanFactory;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息