No FacesContext found in JoinFaces ViewScope

1k Views Asked by At

We are currently migrating a rather big project from JavaEE (Wildfly) to Spring Boot 2.0.5 using JoinFaces 3.2.5 for JSF support. Unfortunately when starting the server we always get the following message:

Scope 'view' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No FacesContext found.

The problematic UI bean is a Spring Component additionally annotated with javax.faces.view.ViewScoped (like class StarterMBean in the joinfaces-maven-jar-example).

Is there anything special we have to be careful about, e.g. forbidden dependencies, special configurations etc?

We are thankful for every hint!

1

There are 1 best solutions below

1
On BEST ANSWER

You have an singleton/application scoped bean which has a direct or indirect dependency on a view scoped bean. This forces the BeanFactory to construct the view scoped bean when the application starts, but view scoped beans can only be used in threads which are currently processing a JSF request.

There are multiple ways to solve this problem:

  1. Try to model your beans to only have dependencies to beans with the same or a higher scope. (So application scoped beans can only use application scoped beans, view scoped beans can use view, session or application scoped ones and so on)
  2. When you are 100% sure your application scoped bean will only use the view scoped one during the processing of a JSF request you can automatically or manually wrap the bean in a scoped proxy.
    • To get a scoped proxy automcatically, change @ViewScoped to @Scope(scopeName = "view", proxyMode = ScopedProxyMode.TARGET_CLASS)
    • If you have no access to the view scoped bean, you can declare the injection point as ObjectProvider<> in order to get a scoped proxy.

More information about this problem can be found here: https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-scopes-other-injection