Showing posts with label Bean. Show all posts
Showing posts with label Bean. Show all posts

Monday, October 22, 2012

Basic Constructor Injection


Through the Spring beans XML file you can configure your bean to initialize with an argument for the constructor, and then assign the arguments. Spring essentially “injects” the argument into your bean. This is referred to as constructor injection. The following example passes in the String message using a constructor. The class is the same as the one in Basic Bean Creation except the default message on the message variable has been cleared and is now set to null. A single parameter constructor has been added to set a message.

ConstructorMessage .java

public class ConstructorMessage {
  private String message = null;
  public ConstructorMessage(String message) {
    this.message = message;
  }
  public void setMessage(String message) {
    this.message = message;
  }
  public String getMessage() {
   return message;
  }
}

The constructor-arg element injects a message into the bean using the constructor-arg element's value attribute.

ConstructorMessageTest-context.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="message" class="org.springbyexample.di.xml.ConstructorMessage">
    <constructor-arg  value="Spring is fun." />
  </bean>
</beans>

Basic Bean Creation in Spring


A Spring bean in the IoC container can typically be any POJO (plain old java object). POJOs in this context are defined simply as reusable modular components – they are complete entities unto themselves and the IoC container will resolve any dependencies they may need. Creating a Spring bean is as simple as coding your POJO and adding a bean configuration element to the Spring XML configuration file or annotating the POJO, although XML based configuration will be covered first.

we'll use a simple POJO, a class called Message which does not have an explicit constructor, just a getMessage() and setMessage(String message) method. Message has a zero argument constructor and a default message value.

DefaultMessage .java

public class DefaultMessage {
  private String message = "Spring is fun.";
  public String getMessage() {
    return message;
  }
  public void setMessage(String message) {
    this.message = message;
  }
}

The bean element below indicates a bean of type Message – defined by the class attribute – with an id of 'message'. The instance of this bean will be registered in the container with this id.

DefaultMessageTest-context.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="message" class="org.springbyexample.di.xml.DefaultMessage" />
</beans>

When the container instantiates the message bean, it is equivalent to initializing an object in your code with 'new DefaultMessage()'

Enjoy Creating Beans in Spring :) :) :)