The Spring IoC container also supports dependency injection by setter method, which is the preferred method of dependency injection in Spring. The property subelement of bean is used for setter injection.

  • Students.java
  • applicationContext.xml
  • Test.java
  • Students.java

    The Students bean class has three attributes like age, name and city. All the three attributes are set thru its setters and getters and a method to show() disply these informations.

    applicationContext.xml

    We are providing the information into the bean by this file. The property element invokes the setter method. The value subelement of property will assign the specified value.

    Test.java

    This class gets the bean from the applicationContext.xml file and calls the show() method.

    Output

    Age:20 Name:Smith City:Australia
    It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

    Leave a Reply

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