cse
cse

Reputation: 4104

What is the object creation policy in spring framework

I am new to spring framework and was just doing some fun with it. During that I encountered a weird output. I tried to debug. But didn't got answers (though I didn't went deep into the framework code).

What I wanted to do: I just wanted to create an instance of User class. With value {city = 'Sirsa2', i1 = 12}.

I have following four code files:

IntTest.java

package Task4;

import org.springframework.stereotype.Service;

//Just creating an integer. Since Integer class is final so, I have to extend Number class. So I also needed to override abstract metods. 
@Service
public class IntTest extends Number {
    Integer i1;

    public Integer getI1() {
        return i1;
    }

    public void setI1(Integer i1) {
        this.i1 = i1;
    }

    public IntTest() {
        i1 = 90;
    }
    @Override
    public double doubleValue() {
        return i1.doubleValue();
    }

    @Override
    public float floatValue() {
        return i1.floatValue();
    }

    @Override
    public int intValue() {
        return i1.intValue();
    }

    @Override
    public long longValue() {
        return i1.longValue();
    }
}

Address.java

package Task4;

import org.springframework.stereotype.Service;

@Service
public class Address {
    private String city;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

User.java

package Task4;

import org.springframework.stereotype.Service;

@Service
public class User {

    Address address;
    Integer i1;

    User(Address address, IntTest t1)
    {
        this.address= address;
        i1 = t1.getI1();
        System.out.println("1. Point " + t1.getI1());
    }

    public void getInfo() {
        System.out.println(address.getCity() + " | "+i1.intValue());
    }
}

Config.java

package Task4;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("Task3")
public class Config {}

MainClass.java

package Task4;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainClass {
    public static void main(String args[]) {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
        context.register(Config.class);
        context.refresh();
        Address address1=(Address)context.getBean(Address.class);
        address1.setCity("Sirsa");

        Address address2=(Address)context.getBean(Address.class);
        address2.setCity("Sirsa2");

        IntTest intTest=(IntTest)context.getBean(IntTest.class);
        System.out.println("2. Point " + intTest.getI1());
        intTest.setI1(12);
        System.out.println("3. Point " + intTest.getI1());

        User user=(User) context.getBean(User.class);
        user.getInfo();
    }
}

When I run MainClass.java as JavaApplication then I'm getting following output:

1. Point 90
2. Point 90
3. Point 12
Sirsa2 | 90

But expecting:

2. Point 90
3. Point 12
1. Point 12
Sirsa2 | 12

I'm not sure how objects are being created. But I think that:

I'm using Eclipse(Oxygen) , Java 1.8.0_121, spring-framework-4.3.9.RELEASE and commons-logging-1.2

Upvotes: 1

Views: 436

Answers (1)

Robert Ellis
Robert Ellis

Reputation: 734

By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process.which is during the start of the application .after that it will keep injecting the objects.The objects are by default singleton scope so it will be same for the entire application .yes you are right it will work like this only

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-lazy-init

When does Spring create instances of objects that are injected

since the System.out.println("1. Point " + t1.getI1()); is in the constructor of the user object while starting the application spring is trying to create the object for this class user and the constructor gets called so point1 is getting printed.

if you want the following behaviour 2. Point 90 3. Point 12 1. Point 12 Sirsa2 | 12

set lazy-init="true" on your beans

Upvotes: 1

Related Questions