Springcore Constructor and setter injection to pojos

So, in our previous post we have made our basic spring project to say hello, Now in this post we gonna see how to inject values(properties) to the person pojo using spring constructor and setter based injection. Normally in any java based project Constructor based injection is happen at the time of object creation and setter injection happen with java beans by calling methods and then we give properties. But, in spring project what we do is we specify the beans(pojo) properties in beans.xml and the beans are instantiated by spring container with those values.

Now we have a pojo Person.java this pojo takes two constructor args and a method which takes one argument which we supply those from beans.xml and

Person.java

package com.sourceforopensource.pojos;

public class Person {

private int id;
private String name;

private int taxId;

public Person() {

}

public Person(int id, String name) {
this.id = id;
this.name = name;
}

public void speak() {
System.out.println("Hello! I'm a person.");
}

public void setTaxId(int taxId) {
this.taxId = taxId;
}

@Override

//we use this toString() method to see what happening
public String toString() {
return "Person [id=" + id + ", name=" + name + ", taxId=" + taxId + "]";
}
}

Now we have beans.xml which we supply args to Person.java from it using constructor-args and property name spaces.

beans.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="person" class="com.sourceforopensource.beans">
<constructor-arg value="777" name="id" type="int">
</constructor-arg>
<constructor-arg value="Mary" name="name" type="String"></constructor-arg>
<property value="321" name="taxId">
</property>
</bean>

</beans>

Now have main App.java which this will call beans.xml to bean container where the bean container will create all the beans by seeing beans.xml and also sets properties associated to those beans.

App.java


package com.sourceforopensource.pojos;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class App {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("com/sourceforopensource/beans/beans.xml");

Person person = (Person)context.getBean("person");
person.speak();

System.out.println(person);

((ClassPathXmlApplicationContext)context).close();
}

}

 

output will be:

Hello! I’m a person.

Person[id=777, name=mary, taxid=321]

 

Balakrishna Pendyala

Author: Balakrishna Pendyala

Balakrishna is a Software Engineer working in Media Streaming Domain, and also worked on Banking and Health Care domains. His areas of interest include Object Oriented Design, SOLID Design principles, Restful Web Services and Open Source softwares including Spring, Spring Boot, and Hibernate.

If you found an error, highlight it and press Shift + Enter or click here to inform us.

Leave a Reply

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