Spring BeanFactory Example

By Arvind Rai, July 08, 2022
On this page we will learn using Spring BeanFactory.
1. The BeanFactory is the root interface for accessing a Spring bean container.
2. Normally a BeanFactory will load bean definitions stored in a configuration source and can configure them.
3. Bean factory implementations should support the standard bean lifecycle interfaces as far as possible.
4. Find some implementation classes of BeanFactory interface.
ClassPathXmlApplicationContext
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
ResourceAdapterApplicationContext
XmlWebApplicationContext
5. Find the methods of BeanFactory interface.
getBean : Returns bean instance.
containsBean : Returns boolean. It checks if this bean factory contains a bean definition with given name.
getBeanProvider : Returns a provider for the specified bean.
getType : Determines the type of the bean.
isPrototype : Is this bean a prototype.
isSingleton : Is this bean a singleton.
isTypeMatch : Checks whether the bean with the given name matches the specified type.

getBean() Method

The getBean() is a BeanFactory method that returns the bean instance. It allows following arguments.
1.
<T> T getBean(Class<T> requiredType) throws BeansException 
It returns the bean instance that uniquely matches the given object type, if any.
Parameters: The requiredType is type the bean must match; can be an interface or superclass.
Returns: It returns an instance of the single bean matching the required type.
Throws: If the bean cannot be created, it throws BeansException.
2.
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException 
Parameters:
The requiredType is type the bean must match; can be an interface or superclass.
The args are the arguments to use when creating a bean instance using explicit arguments.
3.
Object getBean(String name) throws BeansException 
Parameters:
The name is the name of the bean to retrieve.
4.
<T> T getBean(String name, Class<T> requiredType) throws BeansException 

Parameters:
The name is the name of the bean to retrieve.
The requiredType is type the bean must match; can be an interface or superclass.
5.
Object getBean(String name, Object... args) throws BeansException 
Parameters:
The name is the name of the bean to retrieve.
The args are the arguments to use when creating a bean instance using explicit arguments.

Example-1

Here we are using ClassPathXmlApplicationContext class that implements BeanFactory interface.
SpringDemo.java
package com.concretepage;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
	public static void main(String[] args) {
	    BeanFactory  beanFactory = new ClassPathXmlApplicationContext("app-config.xml");
	    Student student = (Student)beanFactory.getBean("student");
	    System.out.println(student.getName());
	    System.out.println(student.getAge());
	}
} 
Student.java
package com.concretepage;
public class Student {
  private String name;
  private int age;
  -----
} 
app-config.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.xsd">
           
	  <bean id="student" class="com.concretepage.Student">
	      <property name="name" value="Mohan" />
              <property name="age" value="25" />
	  </bean>    
</beans> 
Output
Mohan
25 

Example-2

Here we are using GenericXmlApplicationContext class that implements BeanFactory interface.
SpringDemo.java
package com.concretepage;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class SpringDemo {
	public static void main(String[] args) {
		Resource resource = new ClassPathResource("app-config.xml");
		BeanFactory beanFactory = new GenericXmlApplicationContext(resource);
		Student student = (Student)beanFactory.getBean("student");
		System.out.println(student.getName());
		System.out.println(student.getAge());
	}
} 

Reference

Interface BeanFactory

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us