what is the purpose of @Context and @Parallel annotation

Viewed 30

I have few beans which needs to be initialized either parallely or eagerly and to do this while reading the docs of micronaut, I noticed @Parallel annotation which can be used to initialize beans parallely and I came across @Context annotation which I am not able to understand clearly.

As for as my understanding is concerned, @Parallel annotation will shutdown the application if any of the beans executing simultaneously fails whereas @Context will not even startup the application if there is any failure.

I just wanted to check if this correct? Also, if I want to do eager initialization of bean rather than on-demand which annotation is the best one to use?

Any help appreciated, thanks in advance!

1 Answers

When you annotate a bean using the Context annotation, it simply means that the bean gets initialised earlier and eagerly (with the Application Context) than beans annotated with e.g. Singleton or Prototype.

Context scope indicates that the bean will be created at the same time as the ApplicationContext (eager initialisation)

With these annotations you define the scope of your bean within the application context.

Parallel does not define a bean scope, but as you found out yourself behaves accordingly to Context.

As a rule of thumb: If you have beans that need to start early, with the application context, annotate them using @Context. If you have a singleton bean with a slow startup path declare it with @Singleton and @Parallel.

This is how I understud the documentation.

Related