Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

spring

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

like image 602
harshit Avatar asked Mar 05 '10 05:03

harshit


2 Answers

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>
like image 169
oxbow_lakes Avatar answered Jan 01 '23 09:01

oxbow_lakes


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

like image 27
Randy Simon Avatar answered Jan 01 '23 07:01

Randy Simon