Spring Setter Dependency Injection(No Arg Constructor)

In Setter based DI(Dependency Injection), Objects define their dependencies via properties(<property> tag) and Container invoke setter methods to set these properties in object instance after invoking a no-argument constructor or no-argument static factory method to instantiate the bean.

To make setter injection work No-Argument Constructor(Default/Explicit) or No-Argument Static factory method is required. In this topic we will discuss about the DI using no arg constructor. Click here to learn Spring DI using Factory Method.

Bean Class(To  Dependency Inject)

 Here we have overriden “toString” method to view meaningfull output for the programmme.

Spring Configuration File

Above bean will be configure in Spring configuring file where we can provide the value to dependency inject in Bean via property tag(Used for setter Injection).

 In Configuration file we use “class” attribute to provide the fully qualified name of the bean’s class.

And “name” attribute is used to create aliases(address1 & address2)


 Execute Application(Dependency Inject Bean)


Output of the above programme

As you can see that No Argument constructor has been invoked first and the setter method to set the values in it.

Environment Used

Tool : Eclipse Indigo

Java : JDK 1.6

Spring : 3.1.1

Jars Required :

  • org.springframework.core-3.1.1.RELEASE.jar
  • org.springframework.context-3.1.1.RELEASE.jar
  • org.springframework.asm-3.1.1.RELEASE.jar
  • org.springframework.beans-3.1.1.RELEASE.jar
  • org.springframework.expression-3.1.1.RELEASE.jar
  • commong-logging-1.1.1.jar

Leave A Comment

Your email address will not be published. Required fields are marked *