W3Schools Learner's Blog

w3Schools programming knowledge summary website

div

12/12/2017

setter method Injection in spring framework

setter method Injection in spring framework is very simple, let's go to learn.
We can inject the dependency by setter method also. The <property> subelement of <bean> is used for setter injection. Here we are going to inject
  1. primitive and String-based values
  2. Dependent object (contained object)
  3. Collection values etc.

Injecting primitive and string-based values by setter method

Let's see the simple example to inject primitive and string-based values by setter method. We have created three files here:

  • Employee.java
  • applicationContext.xml
  • Test.java
Employee.java file
It is a simple class containing three fields id, name and city with its setters and getters and a method to display these informations.
  1. package com.tpyyes;  
  2. public class Employee {  
  3. private int id;  
  4. private String name;  
  5. private String city;  
  6.   
  7. public int getId() {  
  8.     return id;  
  9. }  
  10. public void setId(int id) {  
  11.     this.id = id;  
  12. }  
  13. public String getName() {  
  14.     return name;  
  15. }  
  16. public void setName(String name) {  
  17.     this.name = name;  
  18. }  
  19.   
  20. public String getCity() {  
  21.     return city;  
  22. }  
  23. public void setCity(String city) {  
  24.     this.city = city;  
  25. }  
  26. void display(){  
  27.     System.out.println(id+" "+name+" "+city);  
  28. }  
  29.   
  30. }  
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.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  8.   
  9. <bean id="emp" class="com.tpyyes.Employee">  
  10. <property name="id">  
  11. <value>20</value>  
  12. </property>  
  13. <property name="name">  
  14. <value>Arun</value>  
  15. </property>  
  16. <property name="city">  
  17. <value>ghaziabad</value>  
  18. </property>  
  19.   
  20. </bean>  
  21.   
  22. </beans>  

Test.java
This class gets the bean from the applicationContext.xml file and calls the display method.
  1. package com.tpyyes;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  5. import org.springframework.core.io.*;  
  6.   
  7. public class Test {  
  8.     public static void main(String[] args) {  
  9.           
  10.         Resource r=new ClassPathResource("applicationContext.xml");  
  11.         BeanFactory factory=new XmlBeanFactory(r);  
  12.           
  13.         Employee e=(Employee)factory.getBean("emp");  
  14.         s.display();  
  15.           
  16.     }  
  17. }  
Output:20 Arun ghaziabad

1 comment:

  1. Nice post By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision.
    Digital Marketing Training India

    ReplyDelete

Note: only a member of this blog may post a comment.