Spring ApplicationContext

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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值