(JSolutions.se is a Swedish blog, this post is however intentionally written in English)

This tutorial will show you how to develop the famous ”Hello World” as a HK2 module using NetBeans 6.0/maven.

The very first step is to create a new Maven Project:
step1.PNG

as a Maven Quickstart Archetyp:

step2.PNG

We also must supply the artifact id, group id and the version.
step3.PNG

NetBeans actually creates a Hello world source file named App in the package se.javasolutions.hk2.HelloWorld.

The next thing we need to do is to add the maven repository containing HK2. The pom is located in the Project Files folder. Add the following to pom.xml:

step4.PNG

Next we need to add the dependency to the hk2-maven-plugin. To do this, select Add Library on the Libraries folder in the project. Add the following:

step5.PNG

We also need to add a dependency to hk2 iteself:

step6.PNG

Now we must change the pom file to use the hk2-maven plugin to build and package the the project.

step7.PNG

By default NetBeans assumes that we are using Java 1.4 and will therefor not recognize annotations. Select Properties on the project folder and the category source. Change Source/Binary Format to 1.5/1.6. Close and re-open the project.

Now it is time to create the contract for our hello world service:

step8.PNG

As you can see is an ordinary interface with an @Contract annotation.

The Service implementation is just as simple:

step9.PNG

The App class is modified to implement the interface ModuleStartup.

step10.PNG

The service instance is injected to the service field by the HK2 runtime container.

And the very final thing we must to is to modify run project action to (note the Set properties):

step11.PNG

Now you can run the project.

The complete NetBeans project with sources can be downloaded here.

Feel free to send commends to rikard.thulin(at)ibs.se

References: