Creating Bean in Spring

A Spring IoC container manages one or more beans. iT CAN BE created based on XML configuration file supplied with definitions.

Please follow the below points in mind while configuring bean class (POJO) in configuration file :
  • A package-qualified class name
  • Specify Scope of bean
  • Life Cycle methods
  • References to other beans
The <bean> tag and attributes :
  • class : It is used to create bean object in the Container. It is essential task in Spring framework. There are several ways to create bean object in the container:

    Using constructor :
    <bean id="exampleBean" class="examples.ExampleBean"/>

    Using static factory method :
    <bean id="clientService"
          class="examples.ClientService"
          factory-method="createInstance"/>

    Using instance factory method :
    <!-- the factory bean, which contains a method called createInstance() -->
    <bean id="serviceLocator" class="examples.DefaultServiceLocator">
      <!-- inject any dependencies required by this locator bean -->
    </bean>

    <!-- the bean to be created via the factory bean -->
    <bean id="clientService"
          factory-bean="serviceLocator"
          factory-method="createClientServiceInstance"/>
  • name : Every bean has one or more identifiers. These identifiers must be unique within the container that hosts the bean.
    <alias name="fromName" alias="toName"/>

  • scope : You can specify the scope of bean while configuring in the configuration file.
    • singleton (default): Single bean definition per IoC container.
    • prototype : Single bean definition to any number of object instances.
    • request : Single bean definition to the lifecycle of a single HTTP request.
    • session : Single bean definition to the lifecycle of an HTTP Session.
    • global : Single bean definition to the lifecycle of a global HTTP Session.
  • constructor arguments :Constructor-based DI is accomplished by the container invoking a constructor with a number of arguments, each representing a dependency.
    public class Employee {
    private String eid, name, city;
    private double salary;
    public Employee(String eid, String name, String city, double salary) {
      this.eid = eid;
      this.name = name;
      this.city = city;
      this.salary = salary;
    }
    }

    <bean id="emp" class="techmyguru.spring.Employee">
        <constructor-arg value="A002">
        <constructor-arg value="Suman Kumar">
        <constructor-arg value="Pune" />
        <constructor-arg value="555555" />
    </bean>

  • properties : Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor.
    public class Employee {
    private String eid, name, city;
    private double salary;
    // all getter and setter methods
    }

    <bean id="emp" class="techmyguru.spring.Employee">
        <property name="eid" value="A001" />
        <property name="name" value="Suman Kumar" />
        <property name="city" value="Delhi" />
        <property name="salary" value="999999" />
    </bean>
  • autowiring mode :You can allow Spring to resolve collaborators (other beans) automatically for your bean by inspecting the contents of the ApplicationContext.
    • no (default) : No autowiring.
    • byName : Autowiring by property name.
    • byType : Allows a property to be autowired if exactly one bean of the property type exists in the container.
    • constructor : Analogous to byType, but applies to constructor arguments.
  • lazy-initialization : A lazy-initialized is used to tells the IoC container to create a bean instance when it is first requested, rather than at startup.
    <bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>

  • initialization method : The InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container.
    void afterPropertiesSet() throws Exception;

  • destruction method : The DisposableBean interface allows a bean to get a callback when the container containing it is destroyed.
    void destroy() throws Exception;

  • Creating bean example index