Java Login


Hello guys, is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.

     << Previous
Next >>     

Spring BeanFactory Container

A BeanFactory is like a factory class that contains a collection of beans. The BeanFactory holds Bean Definitions of multiple beans within itself and then instantiates the bean whenever asked for by clients.

  • BeanFactory is able to create associations between collaborating objects as they are instantiated. This removes the burden of configuration from bean itself and the beans client.
  • BeanFactory also takes part in the life cycle of a bean, making calls to custom initialization and destruction methods.

This is the simplest container providing 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 purposes of backward compatibility with the large number of third-party frameworks that integrate with Spring.

Example of Spring BeanFactory Container

package dependencyInjection;

public class AnotherBean {
private AnotherBean() {

public void anotherDisplay(String s) {


The FirstBean bean class has one attributes viz. anotherBean. These attribute are set through via constructor injection.

package dependencyInjection;

public class FirstBean {

private AnotherBean anotherBean;

private FirstBean() {

public void setAnotherBean(AnotherBean anotherBean) {
this.anotherBean = anotherBean;

public void display(String s) {

Here the context.xml file is used to do spring bean configuration. The following code shows how to set a property value through constructor injection. The constructor-arg element within the bean element is used to set the property value through constructor injection.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xsi:schemaLocation="" >

<bean id="anotherB" class="dependencyInjection.AnotherBean">

<bean id="show" class="dependencyInjection.FirstBean">
<property name="anotherBean" ref="anotherB"></property>


package dependencyInjection;

import org.springframework.beans.factory.BeanFactory;

public class ReferenceMain
public static void main(String[] args)
BeanFactory bean = new ClassPathXmlApplicationContext(
FirstBean f = (FirstBean) bean.getBean("show");
f.display("First Bean");

There are following two important points to note about the main program:

  • First step is to create factory object where we used framework API XmlBeanFactory() to create the factory bean and ClassPathResource() API to load the bean configuration file available in CLASSPATH. The XmlBeanFactory() API takes care of creating and initializing all the objects ie. beans mentioned in the configuration file.
  • Second step is used to get 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 actual object. Once you have object, you can use this object to call any class method.

     << Previous
Next >>