Maikel Nait
Maikel Nait

Reputation: 261

JSF 2.2 : a @NoneScoped bean, injected into another broader scoped Managed Bean is NULL in the @PostConstruct

I believe this question may have been answered in some other threads, but so far I still cannot manage to make it work with my configuration.

As far as I understand, a @NoneScoped bean, injected into another one, will live as long as the scope of the Acceptor Bean.

And so far, it it true... except that it seems the Bean is not yet available during the @PostConstruct method of the Acceptor Bean.

For example, let's say we have an base abstract bean BaseScopedBean with the following ManagedProperty injection:

public abstract class BaseScopedBean implements IBaseBean {

  @ManagedProperty(value = "#{resourceBundleProvider}")
  private ResourceBundleProvider resourceBundleProvider;

  public void setResourceBundleProvider(ResourceBundleProvider resourceBundleProvider) {
    this.resourceBundleProvider = resourceBundleProvider;
  }

  public ResourceBundleProvider getResourceBundleProvider() {
    return this.resourceBundleProvider;
  }
}

where ResourceBundleProvider looks just like this:

@ManagedBean ( name = "resourceBundleProvider")
@NoneScoped
public class ResourceBundleProvider {

  public ResourceBundle getBundle(String bundleName) {
      FacesContext context = FacesContext.getCurrentInstance();
      return context.getApplication().getResourceBundle(context, bundleName);
  }

  public String getValue(String bundleName, String key) {

      try {
          return getBundle(bundleName).getString(key);
      } catch (MissingResourceException e) {
          return '!' + key + '!';
      }
  }

  public String getValue(String bundleName, String key, Object... params) {

      try {
          return MessageFormat.format(getBundle(bundleName).getString(key), params);
      } catch (MissingResourceException e) {
          return '!' + key + '!';
      }
  }
}

And then, we define an @ApplicationScoped bean that extends BaseScopedBean, and tries to access the resourceBundleProvider during a @PostConstruct operation

@ManagedBean
@ApplicationScoped
public class MenuBean extends BaseScopedBean {

  @PostConstruct
  public void init() {
      System.out.println(getResourceBundleProvider());
  }
}

The System.out.println(resourceBundleProvider) in the @PostConstruct prints NULL

However, accessing the resourceBundleProvider later on, in a method invoked from a Facelet EL expression, returns a valid created instance, for example.

The question: Is this the expected behaviour ? I believe the resourceBundleProvider managed property should be already available in the @PostConstruct.

I'm using WildFly 8.2.0.Final with Apache Myfaces 2.2.7 , not the original Mojarra implementation.

Any ideas ?

Thanks a lot in advance !!

Upvotes: 0

Views: 2647

Answers (1)

Maikel Nait
Maikel Nait

Reputation: 261

It turns out that , so far, with Apache MyFaces 2.2.7 ( and presumably 2.2.8 ) this seems to be a bug... that was previously fixed in 2.1.x versions !!

With Mojarra ( at least 2.2.12 ), the behavior is the expected one.

Upvotes: 1

Related Questions