- Spring DI - Home
- Spring DI - Overview
- Spring DI - Environment Setup
- Spring DI - Create Project
- Spring DI - IOC Containers
- Spring Dependency Injection
Constructor Based Injection Examples
- Spring DI - Constructor Based
- Spring DI - Inner Beans via Constructor
- Spring DI - Collections via Constructor
- Spring DI - Collection Ref via Constructor
- Spring DI - Map via Constructor
- Spring DI - Map Ref via Constructor
Setter Based Injection Examples
- Spring DI - Setter Based
- Spring DI - Inner Beans Setter
- Spring DI - Collections Setter
- Spring DI - Collection Ref Setter
- Spring DI - Map Setter
- Spring DI - Map Ref Setter
Autowiring Examples
- Spring DI - Autowiring
- Spring DI - Autowiring ByName
- Spring DI - Autowiring ByType
- Spring DI - Autowiring Constructor
Factory Method
Spring DI Useful Resources
Spring DI - ApplicationContext Container
The ApplicationContext is Spring's advanced container. Similar to BeanFactory, it can load bean definitions, wire beans together, and dispense beans upon request. Additionally, it adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by org.springframework.context.ApplicationContext interface.
The ApplicationContext includes all functionality of the BeanFactory, It is generally recommended over BeanFactory. BeanFactory can still be used for lightweight applications like mobile devices or applet-based applications.
ApplicationContext Implementations
The most commonly used ApplicationContext implementations are −
FileSystemXmlApplicationContext − This container loads the definitions of the beans from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.
ClassPathXmlApplicationContext − This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look like bean configuration XML file in CLASSPATH.
WebXmlApplicationContext − This container loads the XML file with definitions of all beans from within a web application.
Example - Usage of ApplicationContext
Let's update the project created in Spring DI - Create Project chapter. We're adding following files −
HelloWorld.java − A dependency class.
MainApp.java − Main application to run and test.
HelloWorld.java
Here is the content of HelloWorld.java file −
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);
}
}
MainApp.java
Following is the content of the second file MainApp.java −
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\\mahes\\eclipse-workspace\\springdi\\src\\main\\resources\\Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
Following two important points should be noted about the main program −
The first step is to create factory object where we used framework API FileSystemXmlApplicationContext to create the factory bean after loading the bean configuration file from the given path. The FileSystemXmlApplicationContext() API takes care of creating and initializing all the objects ie. beans mentioned in the XML bean configuration file.
The second step is used to get the required bean using getBean() method of the created context. This method uses bean ID to return a generic object, which finally can be casted to the actual object. Once you have an object, you can use this object to call any class method.
Beans.xml
Following is the content of the bean configuration file Beans.xml
<?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>
Output
Once you are done with creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message −
Your Message : Hello World!