2

I have this kind of structure in my Maven project:

WEB-INF/lib
  - a.jar
       - Registry.class (@ApplicationScoped, @ManagedBean(eager=true)
  - b.jar
       - Module.class (@ApplicationScoped, @ManagedBean(eager=true)


I placed a logger at the @PostConstruct annotated method on the two classes to determine which is invoked first and after several deployments on the JBossAS7 server, I noticed that there SEEMS to be no specific order in loading these classes. My intention is to ALWAYS have Registry.class loaded before Module.class. But with this class loading behavior, I don't know how to achieve it.

In some instances, Registry.class is being loaded first but in other instances, Module.class gets loaded first even if I just restarted the application server and did no changes on the code.

Now my question is, is there something that I can do to define the order of loading jars within the WEB-INF/lib?


A different perspective:

Can it also be possible that the problem is not within the class loading but with the ApplicationScoped eager ManagedBean? I added a class on a.jar:

- RegistryTwo.class (@ApplicationScoped, @ManagedBean(eager=true)


so that a.jar now contains Registry.class and RegistryTwo.class. With this, I am expecting something like:

(Desired output)

Registry.class is invoked.
RegistryTwo.class is invoked.
Module.class is invoked.

or (I have will have a problem with this.)

Module.class is invoked.
Registry.class is invoked.
RegistryTwo.class is invoked.


BUT in some instances, I am getting this:

RegistryTwo.class is invoked.
Module.class is invoked.

... (Other Processing logs.)

Registry.class is invoked.


According to @BalusC, an ApplicationScoped eager ManagedBean will be auto-instantiated upon application startup ( How do I force an application-scoped bean to instantiate at application startup?) and that happens in my code.

I just wonder:

  1. How does JSF load/create ApplicationScoped eager ManagedBeans? Is there some sort of rule wherein the order is/can be defined?
  2. Why was Registry.class not instantiated before/right after RegistryTwo.class when they are both under the same jar file and they are both ApplicationScoped?
Community
  • 1
  • 1
inxis
  • 51
  • 1
  • 4

1 Answers1

0

There is absolutely no ordering rule.

I suggest to let Registry implement ServletContextListener or maybe ServletContainerInitializer instead. Both are guaranteed to run before before JSF application scoped bean constructions.

In case of ServletContextListener, you could let Registry put itself in the application scope as follows:

@Override
public void contextInitialized(ServletContextEvent event) {
    event.getServletContext().setAttribute("registry", this);
}

It'll be available in JSF/EL the usual way by #{registry}.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555