In this post, we will look into Spring BeanFactory Container with an Example
The Spring IOC container
Spring IOC container is mainly responsible for instantiating, configuring, and assembling the Spring beans. The Spring container gets instructions on what are objects to instantiate, configure, and assemble by reading configurational metadata. The configurational metadata is represented in XML config file or Java annotations
Spring framework provides the following two types of containers.
- BeanFactory container
- ApplicationContext container
In this post, we post let’s have a look into Spring BeanFactory container using a demo Project.
Spring BeanFactory Container
- This is the simplest container that provides the basic support for DI and defined by the org.springframework.beans.factory.BeanFactory interface.
- XmlBeanFactory is the implementation class of BeanFactory
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<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>com.kkhindigyan.org</groupId> <artifactId>SpringBeanFactoryContainer</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <org.springframework.version>5.2.13.RELEASE</org.springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencies> </project> |
Message.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kkhindigyan.model; public class Message { private Integer messageId; private String message; public Integer getMessageId() { return messageId; } public void setMessageId(Integer messageId) { this.messageId = messageId; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } |
ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="message" class="com.kkhindigyan.model.Message"> <property name="messageId" value="101"></property> <property name="message" value="Welcome to Spring Framework!!"></property> </bean> </beans> |
ClientTest.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 34 |
package com.kkhindigyan.org; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import com.kkhindigyan.model.Message; public class ClientTest { public static void main(String[] args) { BeanFactory beanFactory = null; try { //Creating Instance of BeanFactory Spring Container Resource resource = new ClassPathResource("ApplicationContext.xml"); beanFactory = new XmlBeanFactory(resource); //Asking Spring Container to return Spring bean with id "message" Object object = beanFactory.getBean("message"); //Covert Spring bean into your business Object Message message = (Message)object; //Print Spring bean state System.out.println(message.getMessageId()+"\t"+message.getMessage()); } catch (Exception e) { e.printStackTrace(); } } } |
If you run ClientTest.java as Java Application then it will give the below output:
101 Welcome to Spring Framework!!
That’s all about Spring BeanFactory Container Example
You May Also Like:
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?
If you have any feedback or suggestion please feel free to drop in below comment box.