Tutorials Spring Spring Constructor Injection Using constructor-arg

Spring Constructor Injection Using constructor-arg

In our previous example, we have seen how to create POJO Instances from the IoC Container. In this tutorial, we will examine how to create a POJO instance or bean in the Spring IoC container by invoking its constructor.

Creating bean instance by invoking constructor is the common and direct way of creating beans. It is similar to creating the class instance using the new operator. All you have to do is to define a POJO class with a constructor and use the configuration.

In this example, I will use the following POJO class to demonstrate how to create POJOs by invoking the constructor.

public class Car {
    private String model;
    private double price;

    public Car() { }

    public Car(String model, double price) {
        this.model = model;
        this.price = price;

    public String getModel() {
        return model;

    public void setModel(String model) {
        this.model = model;

    public double getPrice() {
        return price;

    public void setPrice(double price) {
        this.price = price;

To define the above Car bean in the Spring IoC container, you can add the following code snippet to bean.xml configuration file.


  • Note that, if more then one elements is specified, Spring invokes the most appropriate constructor that matches your arguments.
  • Use Spring’s element to define constructor arguments, then for each element Spring injects the value through the setter method.
  • If no is specified to the bean configuration, the default constructor with no arguments is invoked.
  • You can specify the type and index attributes or the name attribute for the element to avoid constructor ambiguity.

You can write the following Main class to test your Cars bean initialization by retrieving the bean instance from the Spring IoC container:

public class Main {
    public static void main(String[] args) throws Exception {
        ApplicationContext context = new GenericXmlApplicationContext("beans.xml");
        Car car = (Car) context.getBean("carBean");

Inline Feedbacks
View all comments
Previous Post
Bootstrap3 Tutorials and Examples

Bootstrap Responsive Classes for Building Adaptive Layouts

Next Post

Batch Update Operation Using Spring JdbcTemplate

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
Would love your thoughts, please comment.x