Spring ApplicationContext

Table of Contents

This is 12 of 16 parts of tutorial series:Spring ApplicationContext

Tutorial Content: Spring tutorial for beginners

In this tutorial we will learn about what is ApplicationContext and how we can access it.

ApplicationContext:

ApplicationContext is an central interface for providing configuration information to an application.

An ApplicationContext provides the following functionalities:

  • Bean factory methods, inherited from ListableBeanFactory. This avoids the need for applications to use singletons.
  • The ability to resolve messages, supporting internationalization. Inherited from the MessageSource interface.
  • The ability to load file resources in a generic fashion. Inherited from the ResourceLoader interface.
  • The ability to publish events. Implementations must provide a means of registering event listeners.
  • Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.

 ApplicationContext vs BeanFactory:

We can get our bean from both by applying getBean method.BeanFactory is subset of ApplicationContext and provides less functionalities.So if we want to use full functionalities then we go for ApplicationContext.

Getting ApplicationContext in Bean Class:

To get access to ApplicationContext we should implement ApplicationContextAware interface in the respective java bean.

ApplicationContextAware  Interface:

 It has a method,
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.

Implementing ApplicationContextAware interface makes sense for example when an object requires access to a set of collaborating beans. Note that configuration via bean references is preferable to implementing this interface just for bean lookup purposes.

This interface can also be implemented if an object needs access to file resources, i.e. wants to call getResource, wants to publish an application event, or requires access to the MessageSource. However, it is preferable to implement the more specific ResourceLoaderAware, ApplicationEventPublisherAware or MessageSourceAware interface in such a specific scenario.

Spring Application context example:

For configuring spring in your eclipse ide please refer  hello world example

1.Country.java:

This is simple pojo class having some attributes so here country has name and object of Capital class.
Create Country.java under package org.arpit.javapostsforlearning.Copy following content into Country.java.

2.Capital.java

This is also simple pojo class having one attribute called “capitalName”.

Create Capital.java under package org.arpit.javapostsforlearning.java.Above Country class contains object of this class.Copy following content into Capital.java

4.ApplicationContext.xml

[crayon-6725b6ab1d696739518201/]

As you can see, there is no connection between above two beans in this XML file.We are getting object of country class with help of getBean method and then passing id of  capital class to getCapitalName method of country class.In getCapital method,we have ApplicationContext object which is initialized by setApplicationContext method by spring container ,so with help of ApplicationContext object,we are calling getBean method for initializing capital object and getting capitalName from that object.

4.ApplicationContextMain.java

This class contains main function.Create ApplicationContextMain.java under package org.arpit.javapostsforlearning.Copy following content into ApplicationContextMain.java
You can note here that we have used ClassPathXmlApplicationContext for getting bean here.There are various ways for getting beans.In hello world example we have used XmlBeanFactory for getting beans.

5.Run it

When you will run above application,you will get following as output.

That’s all about Spring ApplicationContext.

Was this post helpful?

Comments

  1. Hi

    I think that there is an error in your code. You hadn't set the applicationContext of the country object. So it's null.

    To avoid it, you must write “countryObj.setApplicationContext(appContext);” after getting country's bean

    Tell me if I'm wrong…

    Sorry for my english and congratulations for your tutorial. It's really useful!

    1. Hi Mikel,
      There no need to call “countryObj.setApplicationContext(appContext);” because when you implements ApplicationContextAware interface,it internally call set method and set ApplicationContext.
      Thank you for your comment.I will update this in article.

  2. Great tutorial.
    Just a kinf FYI: I think the final output is wrong. Instead of “India's Capital Name:Delhi”, it is actually outputting “Capital Name:Delhi”.

  3. So, I guess to my understanding, implementing the interface on the Country bean is an alternative to making the Capital bean an inner bean in the xml. Because if I make the Capital bean an inner bean, then I don't have to implement ant interface. Let me know if I am wrong.

  4. Hi, do you have any guide when to use which XmlApplicationContext?

    I'm new to Spring and I have no idea when to use GenericXmlApplicationContext, ClassPathXmlApplicationContext, etc.

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *