0

I came across the following issue: I have the following JSF bean:

public class MailingBean {
    private RecipientService<?> recipientService;
    private SearchRecipientController searchRecipientController = new SearchRecipientController(recipientService);

    //Stuff, GET, SET
}

That bean's intialized in the faces-config.xml as follows:

<managed-bean>
    <managed-bean-name>mailingBean</managed-bean-name>
    <managed-bean-class>path.to.package.MailingBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>recipientService</property-name>
        <value>#{playerService}</value>
    </managed-property>
</managed-bean>

The thins is when new SearchRecipientController(recipientService) is invoking, recipientService is null. Is it possible to specify initialization order in some way? Or how can I fix that?

user3663882
  • 6,957
  • 10
  • 51
  • 92

1 Answers1

1

You cannot use the managed property in the constructor(or as in your case in declaration). You should use a postconstruct method for what you are trying to do. The manage property is set after the object is constructed.

private SearchRecipientController searchRecipientController;

@PostConstruct
public void init(){
     searchRecipientController = new SearchRecipientController(getRecipientService());
}

Another way would be to use the searchRecipientController only via getter.

public SearchRecipientController getSearchRecipientController(){
    if(searchRecipientController==null){
        searchRecipientController = new SearchRecipientController(getRecipientService());
    }
    return searchRecipientController;
}
NeplatnyUdaj
  • 6,052
  • 6
  • 43
  • 76