Table of Contents
- Tutorial Content: Spring tutorial for beginners
- ApplicationContext:
- Getting ApplicationContext in Bean Class:
- 1.Country.java:
- 2.Capital.java
- 4.ApplicationContext.xml
1234567891011121314<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="country" class="org.arpit.javapostsforlearning.Country" ><property name="countryName" value="India"/></bean><bean id="capital" class="org.arpit.javapostsforlearning.Capital" ><property name="capitalName" value="Delhi"/></bean></beans>
- 5.Run it
This is 12 of 16 parts of tutorial series:Spring ApplicationContext
Tutorial Content: Spring tutorial for beginners
- Introduction to spring framework
- Spring interview questions
- Dependency injection(ioc) in spring
- Spring XML based configuration example
- Spring java based configuaration
- Dependency injection via setter method in spring
- Dependency injection via constructor in spring
- Spring Bean scopes with examples
- Initializing collections in spring
- Beans Autowiring in spring
- Inheritance in Spring
- Spring ApplicationContext
- Spring lifetime callbacks
- BeanPostProcessors in Spring
- Annotation based Configuration in spring
- Spring AOP tutorial
In this tutorial we will learn about what is ApplicationContext and how we can access it.
ApplicationContext:
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:
Getting ApplicationContext in Bean Class:
ApplicationContextAware Interface:
1 2 3 4 5 |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package org.arpit.javapostsforlearning; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class Country implements ApplicationContextAware{ String countryName ; ApplicationContext applicationContext; Capital capital; public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext=applicationContext; } public String getCapitalName(String capitalBeanName) { capital=(Capital) applicationContext.getBean(capitalBeanName); String capitalName=capital.getCapitalName(); return capitalName; } } |
2.Capital.java
Create Capital.java under package org.arpit.javapostsforlearning.java.Above Country class contains object of this class.Copy following content into Capital.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.javapostsforlearning; public class Capital { String capitalName; public String getCapitalName() { return capitalName; } public void setCapitalName(String capitalName) { this.capitalName = capitalName; } } |
4.ApplicationContext.xml
[crayon-67266cbfcab49098278218/]
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
12345678910111213141516
package org.arpit.javapostsforlearning; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; public class ApplicationContextMain{ public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Country countryObj = (Country) appContext.getBean("country"); System.out.println("Capital Name:"+countryObj.getCapitalName("capital")); }}
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.javapostsforlearning; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ApplicationContextMain{ public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Country countryObj = (Country) appContext.getBean("country"); System.out.println("Capital Name:"+countryObj.getCapitalName("capital")); } } |
5.Run it
1 2 3 |
India's Capital Name:Delhi |
That’s all about Spring ApplicationContext.
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!
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.
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”.
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.
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