Using Apache OpenWebBeans in Java SE

Daniel Dias
Daniel Dias
Published in
3 min readApr 15, 2020
https://openwebbeans.apache.org/resources/images/logo.png

This post have objective to show how configure the Apache OpenWebBeans in our Java SE applications . So, this post will be short : )

This is my first article in English, Please, enjoy the subject . And sorry by the grammatical mistakes.

Introduction at Apache OpenWebBeans

Apache OpenWebBeans is an other CDI(Contexts and Dependency injection) implementation for Jakarta EE , in your current version implements the CDI 2.0 .

Different of Weld that is the reference implementation, that may be is more known , the OpenWebBeans too have your the light : )

According with the site , OpenWebBeans is :

  • Fast :we agressively use caches internally and deliver great performance
  • Modular : OpenWebBeans Core is purely JavaSE, additional EE functionality gets added via ‘Modules’
  • Industry Proven : Many projects use OpenWebBeans in production.
  • Community Oriented : Please visit our mailing list and we will help you moving your project forward.

The application Server how TomEE use the Apache OpenWebBeans how your CDI container .

Next step is create an simple Hello World application for use the OpenWebBeans in Java SE .

Creating the project

First create a new maven project with the following pom.xml:

Here we have two dependencies , one is openwebeans-se and other is the spec of CDI 2.0 .

The openwebeans-se contains the jars needed for use the CDI that is :

He is very similar at weld-se-shaded for Weld .

too in folder resources/META-INF , create an file beans.xml with the content :

Creating the Classes

Now we are will create an class with name MyBean :

Here I create an simple class that contains a unique method getHelloBean() and in top of our class utilize the annotation @Named that is necessary to your correct working com OpenWebBeans in Java SE .

Now we are will create the Main Class with the following content and is where the magic happens :

This is a simple class main that makes the configuration to use the OpenWebBeans container .

In line 13 I create an attribute static of ContainerLifecycle class and and start the same receiving null.

In line 17 this attribute receive a new instance of Container LifeCycle and line 18 I call the method .startApplication(null); for start the our container OWB .

Once starting the OWB is need get the our bean CDI .

So in line 20 we create an BeanManager that receive the our lifeCycle and will call the method getBeanManager() .

In line 21 we create an new attribute for get an bean active and so will return an set of bean. Here will return the name of our MyBean.

In line 23 get the reference of our bean and create the instance of MyBean

and finally in line 25 we make the use of method getHelloBean(String name) and the result in console is “” :

Perfect, now we have the Apache OpenWebBeans working in Java SE .

Other form of make more easy is using the API of CDI 2.0 or use o module Apache Deltaspike (deltaspike-cdictrl).

that is all for today : )

CODE : https://github.com/Daniel-Dos/danieldiasjava-medium-english/tree/master/Using-Apache-OpenWebBeans-in-Java-SE

--

--

Daniel Dias
Daniel Dias

SouJava Board Member, JCP Member, JSR-371 (MVC 1.0), JSR-382 (Config) specifications contributor, EG JSR-385 (UoM) and Eclipse Committer .