Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Spring ApplicationContext container


May 14, 2021 Spring


Table of contents


Spring ApplicationContext container

Application Context is a child interface of BeanFactory, also known as the Spring context.

Application Context is a higher-level container in spring. S imilar to BeanFactory, it loads the beans defined in the configuration file, brings all beans together, and assigns beans when requested. I n addition, it adds the functionality that enterprises need, such as parsing text information from property files and passing events to the specified listener. This container is defined in the org.springframework.context.ApplicationContext interface interface.

ApplicationContext includes all of BeanFactory's features, and in general, ApplicationContext is better than BeanFactory. Of course, BeanFactory can still be used in lightweight applications, such as mobile devices or applet-based applications.

The most commonly used ApplicationContext interface implementation:

  • FileSystemXmlApplicationContext: The container loads a defined bean from an XML file. Here, you need to provide the constructor with the full path to the XML file.

  • ClassPathXmlAppingContext: The container loads a defined bean from an XML file. Here, you don't need to provide the full path to the XML file, just configure the CLASSPATH environment variable correctly, because the container searches for the bean profile from CLASSPATH.

  • WebXmlApplicationContext: The container loads a bean that has been defined in an XML file within the scope of a web application.

We've seen the ClassPathXmlApplicationContext container in the Spring Hello World Example section, and we've discussed a lot about WebXmlAppingContext in this separate section of Spring-based web applications. So, next, let's look at an example of FileSystemXmlApplicationContext.

Example:

Let's say we've installed eclipse IDE, and following the steps below, we can create a Spring application.

Steps Describe
1 Create a project called SpringExample and create a new folder src under src called com.tutorialspoint
2 Right-click to select the Add External JARs option to import Spring's library files, as we mentioned in the Spring Hello World Example section.
3 Create helloworld files and MainApp files .java the com.tutorialspoint .java and MainApp.
4 Beans, the profile under which the bean was created, .xml.
5 The final step is to edit the contents of all JAVA files and bean's profile to run the application as we said before.

Here's what the .java HelloWorld does:

package com.tutorialspoint;
public class HelloWorld {
   private String message;
   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

Here's what the .java MainApp reads:

package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new FileSystemXmlApplicationContext
            ("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
   }
}

In the main program, we need to pay attention to the following two points:

  • The first step is to generate the factory object. A fter loading the bean profile under the specified path, build the factory bean using the FileSystemXmlApplicationContext API provided by the framework. FileSystemXmlApplicationContext is responsible for generating and initializing all objects, such as all beans in the XML bean profile.

  • The second step takes advantage of the getBean() method in the context generated by the first step to get the required bean. T his method returns a real object through the bean ID in the configuration file. Once you get this object, you can use it to call any method.

Here's what'.xml in the profile 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="com.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

If you've finished the above, next, let's run this application. If the program is error-correct, you will see the following information from the console:

Your Message : Hello World!