I have an action class and that class will have a BOC object that will fill with Spring Dependency Injection. Below shows the sample code:
public class ActionCls {
private BOC theBoc = null;
/*** theBoc getter / setter ***/
}
If I want to call a member function, say thefunc, that belong to BOC inside the ActionCls constructor, like this:
public class ActionCls {
private BOC theBoc = null;
ActionCls() {
theBoc.thefunc();
}
}
a runtime error will be thrown saying that theBoc is null. I did try to use init-method in spring configuration like this:
<bean id="theBoc" class="com.huahsin68.BOC" init-method="thefunc"></bean>
Anyhow this doesn’t help because even though thefunc is get called first, but the theBoc setter is invoke only after ActionCls constructor. Is that a way to call theBoc setter 1st then only ActionCls constructor? So that theBoc is not null and I can invoke thefunc.
You can’t expect to call a getter or setter on a class BEFORE its constructor is called. What you want to achieve is not possible. You can better create a parametrized constructor, in which you init theBoc with some argument, using the
constuctor-argparam on your ActionCls bean, then call the method:Then you can define your constructor arg like that: