tags:

views:

37

answers:

3

This is a basic question - when spring bean is loaded and if i have a constructor and setters which one will be called first?

Thanks

+2  A: 

Doesn't the constructor have to be called first? The setters are instance methods so that can't called until the object is instantiated.

Randy Simon
A: 

I don't think Spring provides any guarantees about the order in which setters are called. It would be good practice to make your beans work regardless of which order the setters are called. If you want to do some processing after all the setters have been called, you might find that it's convenient to use a post construction method. Or if you are using XML configuration rather than annotations, an initialization method might suit.

John
+1  A: 

The constructor must be called before any setter methods are called. Use the init-method to tell Spring to invoke some logic after the setters are called:

<bean class="my.CoolClass" init-method="startup">
    <constructor-arg value="Foo" />
    <property name="bar" value="baz" />
</bean>
oxbow_lakes