- Spring - Home
- Spring - Overview
- Spring - Architecture
- Spring - Environment Setup
- Spring - Hello World Example
- Spring - IoC Containers
- Spring - Bean Definition
- Spring - Bean Scopes
- Spring - Bean Life Cycle
- Spring - Bean Post Processors
- Spring - Bean Definition Inheritance
- Spring - Dependency Injection
- Spring - Injecting Inner Beans
- Spring - Injecting Collection
- Spring - Beans Auto-Wiring
- Annotation Based Configuration
- Spring - Java Based Configuration
- Spring - Event Handling in Spring
- Spring - Custom Events in Spring
- Spring - AOP with Spring Framework
- Spring - JDBC Framework
- Spring - Transaction Management
- Spring - Web MVC Framework
- Spring - Logging with Log4J
Spring Useful Resources
Spring - BeanFactory Container
This is the simplest container providing the basic support for DI and defined by the org.springframework.beans.factory.BeanFactory interface. The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring for the purpose of backward compatibility with a large number of third-party frameworks that integrate with Spring.
There are a number of implementations of the BeanFactory interface that are come straight out-of-the-box with Spring. The most commonly used BeanFactory implementation was the XmlBeanFactory class.It is now deprecated and is not used anymore. This container read the configuration metadata from an XML file and used it to create a fully configured system or application.
The BeanFactory was usually preferred where the resources are limited like mobile devices or applet-based applications. ApplicationContext is the most preferred option unless you have a good reason for not doing so.
Example - Usage of BeanFactory Container
Let us take a look at a working Eclipse IDE in place and take the following steps to create a Spring application −
| Steps | Description |
|---|---|
| 1 | Create a Maven project with a name spring, groupid com.tutorialspoint, artifactid spring and create a package com.tutorialspoint under the src folder in the created project. |
| 2 | Update the pom.xml as explained in the Spring - Environment Setup chapter. |
| 3 | Create Java classes HelloWorld and MainApp under the com.tutorialspoint package. |
| 4 | Create Beans configuration file Beans.xml under the src/main/resources folder. |
| 5 | The final step is to create the content of all the Java files and Bean Configuration file. Finally, run the application as explained below. |
HelloWorld.java
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
package com.tutorialspoint;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
BeanFactory context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
Notable Points
Following two important points should be noted about the main program −
The first step is to create a factory object where we used the framework API ClassPathXmlApplicationContext() to create the factory bean and to load the bean configuration file available in CLASSPATH. The ClassPathXmlApplicationContext() API takes care of creating and initializing all the objects, i.e. beans mentioned in the configuration file.
The second step is used to get the required bean using getBean() method of the created bean factory object. This method uses bean ID to return a generic object, which finally can be casted to the actual object. Once you have the object, you can use this object to call any class method.
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 the 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!