Reputation: 1733
If we inject a variable value through spring and also initialize that in the class itself then what will be picked and why? E.g.
public class Test {
String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Springcontext.xml
<bean id="test" class="com.testpac.Test"
<property name="message" value="i am message text" />
</bean>
Upvotes: 0
Views: 516
Reputation: 12985
Spring calls the constructor first and then calls the setter methods on the instance constructed.
If you mean you would set the value in the initialization where the variable is declared or in the constructor, the Spring set value will take precedence.
If you mean you call the setter after getting the bean from Spring, that will overwrite whatever Spring set it to.
The former is code like this:
public class Test {
String message = "Initialized value in code";
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
And the value will be what Spring sets it to. "i am message text" in your example.
In the latter case, it looks like this:
Test bean = (Test) appCtx.getBean("test");
bean.setMessage("message set in loaded bean");
And then the value is "message set in loaded bean", instead.
Upvotes: 3