Wednesday 16, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 8 February 2014

Spring BeanFactory Container

In this tutorial we are going to see Spring bean factory container and example program using spring BeanFactory container.
The BeanFactory container provides basic support for Dependency Injection (DI) , which is defined by org.springframework.beans.factory.BeanFactory  interface. Some interfaces related to BeanFactory interfaces are given below.
  1. BeanFactoryAware
  2. InitializingBean
  3. DisposableBean
The above mentioned interfaces are still present in Spring for backward compatibility with large number of third party frameworks that integrate with spring
Most commonly used BeanFactory implementation is XmlBeanFactory class. This implementation reads the configuration metadata from an XML file and uses it into create a fully configured system or application.
The bean factory usually prepared where the resource are limited like mobile devices or applet beased applications.So use ApplicationContext unless you have a good reason for not doing so.
Here example program for BeanFactory given below.follow the steps given below to create Spring application using BeanFactory container.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called XmlBeanFactory.
3. Add the following jars into your build path.

commons-logging-1.1.3.jar
spring-beans-3.2.6.RELEASE.jar
spring-context-3.2.6.RELEASE.jar
spring-context-support-3.2.6.RELEASE.jar
spring-core-3.2.6.RELEASE.jar
spring-expression-3.2.6.RELEASE.jar

4. Now create package com.javatutorialscorner.spring under XmlBeanFactory Project
5. Next create SpringXMLBeanFactory class under com.javatutorialscorner.spring package
SpringXMLBeanFactory.java
package com.javatutorialscorner.spring;

public class SpringXMLBeanFactory {
private String sayHello;

public void getSayHello() {
System.out.println("Java Tutorials Corner " + sayHello);
}

public void setSayHello(String sayHello) {
this.sayHello = sayHello;
}
}

6. Now Create RunApp class which used to load configuration from Bean.xml file

RunApp.java

package com.javatutorialscorner.spring;


import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;




@SuppressWarnings("deprecation")
public class RunApp {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


XmlBeanFactory xmlBeanFactory = new XmlBeanFactory
(new ClassPathResource("Beans.xml"));
SpringXMLBeanFactory springXMLBeanFactory = (SpringXMLBeanFactory)xmlBeanFactory.getBean("beanFactory");
springXMLBeanFactory.getSayHello();
}

}

In above program

First step I created XmlBeanFactory  instance using ClassPathResource which load configuration from bean file  which located in class path of application, It take care of creating and initializing all the objects (Beans) declared in bean.xml

Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.

Once Object Created you can access any method from that class.

7. Create Bean.xml at where your class files created (ClassPathResource  tries to load bean file from class path).

Bean.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="beanFactory" class="com.javatutorialscorner.spring.SpringXMLBeanFactory">
<property name="sayHello" value="say Hello"></property>
</bean>
</beans>

Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.

Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to   variable sayHello without changing the class file.

Now you can run the program see the following output in console.

Feb 09, 2014 11:27:08 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [Beans.xml]

Java Tutorials Corner say Hello

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Spring Bean Overview In this tutorial we are going to see the overview of Spring Bean. A bean is an objects that are managed by the Spring IoC  container.A Spring IoC container manages one or more beans. These beans are created with the con… Read More
  • Spring Destruction callbacksIn this tutorial we are going to see about Spring Life cycle Destruction callbacks with example program.Implementing the org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the cont… Read More
  • Spring Initialization CallbacksIn this tutorial we are going to see Spring Life cycle - Initialization callbacks (init(),InitializingBean ) with example program.The org.springframework.beans.factory.InitializingBean interface allows a bean to perform initi… Read More
  • Spring Hello World Program using Spring Source Tool In this tutorial we are going to see how to create Spring Hello World program .Step by step procedure to create Spring Program using Eclipse and Spring Source tool is given below, before start this tutorials refer http://www… Read More
  • Spring BeanFactory Vs ApplicationContext In this tutorials we are going to see why ApplicationContext container is better than BeanFactory  container. The ApplicationContext includes all the functionality of BeanFactory, it is generally recommended over the Be… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Spring BeanFactory Container Rating: 5 Reviewed By: eHowToNow