Eager loading of a service

Hello everyone,

Is there a way to eagerly initialize a service? without calling Bean.get(Service.class)

I’ve tried the following code, but it does not work:

public class SomeModule extends AxelorModule {
  @Override
  protected void configure() {
    bind(Service.class).to(ServiceImpl.class).asEagerSingleton();
  }
}

ServiceImpl class have only a constructor which passes variables of another bean which is injected.

public class ServiceImpl implements Service {
  @Inject
  public ServiceImpl(SomeService someService) {
    someService.addValidStatus(0); // Adds an int to Set<Integer> variable
    someService.addBlockedName("Some name"); // Adds a string to a List<String> variable
  }
}

It is also acceptable to call Beans.get(Service.class), but I do not know where I can call it safely.

Hello everyone,

Regarding your query about eagerly initializing a service without explicitly calling Bean.get(Service.class), you can achieve this by using the asEagerSingleton() binding in your SomeModule class. However, it seems that your current code is not working as expected.

To troubleshoot the issue, make sure that you have correctly set up your dependency injection framework. If you’re using a framework like Guice, ensure that your SomeModule is properly installed and configured. Additionally, check that all the required dependencies for ServiceImpl are correctly bound.

If you’re using Axelor, as it appears from your code snippet, you might need to verify the initialization order of your modules. Make sure that SomeModule is loaded and initialized before the code that requires the eagerly initialized service is executed.

If you prefer to use Beans.get(Service.class) instead of eager initialization, you can safely call it in the appropriate place within your codebase. Typically, you can access the Service instance after the DI framework has been fully initialized, such as in a method annotated with @PostConstruct or at the entry point of your application.

Ensure that you have a clear understanding of your application’s lifecycle and initialization process to determine the suitable place for calling Beans.get(Service.class).