0

I have a ManagedBean say clsA, inside this clsA has a BOC object that will fill with DI. If I want to invoke the BOC during clsA construction, I will do it in clsA constructor. The code will look like this:

@ManagedBean(name="clsA") 
public class ClsA {

    private BOC boc;

    public clsA(BOC theBoc) {
       theBoc.doFuncA();
    }

    public String doFuncD() { return ""; }       
 }

And the Spring configuration will have this:

<bean id="theBoc" class="com.foo.BOC"/>

<bean id="clsA" class="com.foo.clsA">
   <constructor-arg value="theBoc"/>
</bean>

Now I found a problem that if I have the clsA bean declare inside Spring configuration, my JSF bean, clsA, which is also same name with the one declare inside Spring configuration, will not work. Meaning that if I invoke the doFuncD() from JSF, it is not get call. If I remove the clsA Spring declaration, the JSF bean clsA is working fine.

Is there a better way to invoke BOC from managedBean ClsA constructor?

2
  • 1
    It look like you're mixing the responsibilities of JSF and Spring. I don't do Spring and I have no idea why you're using Spring that way wherein you completely kill the JSF bean management facility by suppressing the default constructor, so I can't answer that part, but you should be able to use @PostConstruct annotation to perform logic directly after construction and dependency injection. See also among others stackoverflow.com/questions/10196982/…
    – BalusC
    Aug 12, 2012 at 12:45
  • I agree with you this is my code design problem. I really need to rethink about it.
    – huahsin68
    Aug 13, 2012 at 0:50

1 Answer 1

0

My work around on this question is:

  1. I'll remove the constructor code. No more Constructor level dependency injection.
  2. Move the theBoc.doFuncA(); from constructor to setter of ClsA.
  3. Remove the Spring bean, clsA, declaration from Spring configuration file.

Here is the revised code:

@ManagedBean(name="clsA") 
public class ClsA {

  private BOC boc;

  public clsA(BOC theBoc) {
  }

  public String doFuncD() { return ""; }

  public void setBoc(BOC boc) {
    this.boc = boc;
    theBoc.doFuncA();
  }
}

But how do I ensure the setter will only get invoke once only?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.