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

Before beginning, make sure that you have the IDE's Maven support. You can install the Maven plugin by choosing Tools > Plugins, clicking the Available Plugins tab, selecting the Maven checkbox, and clicking Install.

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

as a Maven Quickstart Archetyp:

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

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:

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:

We also need to add a dependency to hk2 iteself:

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

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:

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

The Service implementation is just as simple:

The App class is modified to implement the interface ModuleStartup.

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):

Now you can run the project.

The complete NetBeans project with sources can be downloaded here: HK2/helloworld.zip(info).

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

References:

  • HK2 development site
  • GlassFish v3 Engineering Guide
  • HK2 presentation by Rikard Thulin & Ferid Sabanovic, IBS JavaSolutions

Attachments

helloworld.zip Info on helloworld.zip 14037 bytes
step1.PNG Info on step1.PNG 92452 bytes
step10.PNG Info on step10.PNG 28558 bytes
step11.PNG Info on step11.PNG 27660 bytes
step2.PNG Info on step2.PNG 76860 bytes
step3.PNG Info on step3.PNG 58506 bytes
step4.PNG Info on step4.PNG 26826 bytes
step5.PNG Info on step5.PNG 10954 bytes
step6.PNG Info on step6.PNG 10602 bytes
step7.PNG Info on step7.PNG 17703 bytes
step8.PNG Info on step8.PNG 18958 bytes
step9.PNG Info on step9.PNG 21010 bytes