MicroBean Main

This is the fifth of a series of posts on some of the personal projects I’ve been working on.  As I hope you’ll see, they all fit together.  The previous post covered MicroBean CDI Utilities.  The next post covers MicroBean Jersey Container Grizzly HTTP CDI Integration.

This post covers MicroBean Main.  Its website is here, its source code is here and its binaries are available from Maven Central.

MicroBean Main provides a public static void main(String[]) method that you can use in your CDI 2.0 Java SE applications.  It is staggeringly simple.  It starts a CDI container, making the command line arguments available to the CDI environment in a portable fashion, and then stops the container.

To install MicroBean Main, place it on the classpath.

That doesn’t sound like much, but as I’ve written previously, that, coupled with the event mechanism that CDI defines that notifies CDI beans when the container is started and stopped, provides a very useful mechanism for integration.  Basically, you don’t have to write your own public static void main(String[]) method or rely on an application server for a CDI environment to be made available to your CDI bean.  If your CDI bean observes the initialization of the application scope context, like, perhaps, this:

private final void onStartup(@Observes @Initialized(ApplicationScoped.class) final Object event) {
  // Here is your "main" code.

…then when MicroBean Main‘s Main#main(String[]) method is run your observer method will be invoked.  See my prior post on eager startup for more.

In the next post, we switch gears slightly and talk about Jersey and Grizzly.

Author: Laird Nelson

Devoted husband and father; working on Helidon at the intersection of Java, Jakarta EE, architecture, Kubernetes and microservices at Oracle; open source guy; Hammond B3 player and Bainbridge Islander.

%d bloggers like this: