Spring ApplicationContext Example

The ApplicationContext is the central interface within a Spring application for providing configuration information to the application. The ApplicationContext interface provides the getBean() method to retrieve bean from the spring container.

It implements the BeanFactory interface. Hence ApplicationContext includes all functionality of the BeanFactory and much more!
Check out BeanFactory vs ApplicationContext in Spring article to know the difference between BeanFactory and ApplicationContext.
Its main function is to support the creation of big business applications.
Features:
  • Bean instantiation/wiring
  • Automatic BeanPostProcessor registration
  • Automatic BeanFactoryPostProcessor registration
  • Convenient MessageSource access (for i18n)
  • ApplicationEvent publication
The ApplicationContext interface uses eager loading, so every bean instantiate after the ApplicationContext started up.

ApplicationContext interface implementations

Spring provides many ApplicationContext interface implementations that we use are;
  1. AnnotationConfigApplicationContext: If we are using Spring in standalone Java applications and using annotations for Configuration, then we can use this to initialize the container and get the bean objects.
  2. ClassPathXmlApplicationContext: If we have spring bean configuration XML file in a standalone application, then we can use this class to load the file and get the container object.
  3. FileSystemXmlApplicationContext: This is similar to ClassPathXmlApplicationContext except that the XML configuration file can be loaded from anywhere in the file system.
AnnotationConfigWebApplicationContext and XmlWebApplicationContext for web applications.

ApplicationContext interface Example

Let's write code to create a Spring container using the ApplicationContext interface:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Note that we are supplying configuration metadata via applicationContext.xml file(XML-based configuration).
AnnotationConfigApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
Note that we are supplying configuration metadata via AppConfig.class file.
The ApplicationContext interface provides a getBean() method to retrieve bean from the spring container.
ApplicationContext getBean() Example:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
Let's develop step by step a complete example to demonstrate the usage of the Spring ApplicationContext interface.

1. Create a simple Maven Project

Create a simple maven project using your favorite IDE and refer below diagram for packaging structure. If you are new to maven then read this article How to Create a Simple Maven Project.

Project Structure

Below diagram shows a project structure for your reference - 

2. Add Maven Dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.javaguides.spring</groupId>
    <artifactId>spring-ioc-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3. Configure HelloWorld Spring Beans

What Is a Spring Bean?

This is a very simple question that is often overcomplicated. Usually, Spring beans are Java objects that are managed by the Spring container.
Here is a HelloWorld Spring bean:
package net.javaguides.spring.ioc;

public class HelloWorld {
    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public void getMessage() {
        System.out.println("My Message : " + message);
    }
}

Configuration Metadata - Configure HelloWorld Spring Beans

<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
   
 <bean id="helloWorld" class="net.javaguides.spring.ioc.HelloWorld">
  <property name="message" value="Hello World!" />
 </bean>
</beans>

4. Create a Spring Container

If we have spring bean configuration XML file in a standalone application, then we can use ClassPathXmlApplicationContext class to load the file and get the container object.
package net.javaguides.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
}

5. Retrieve Beans from Spring Container

ApplicationContext interface provides getBean() method to retrieve bean from the spring container.
package net.javaguides.spring.ioc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
        obj.getMessage();
    }
}

Output

My Message : Hello World!

Comments