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


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;


//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.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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 value="Mary" name="name" type="String"></constructor-arg>
<property value="321" name="taxId">


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.


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");





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 *