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
.
Feel free to send commends to rikard.thulin(at)ibs.se
References:
| helloworld.zip | ![]() |
14037 bytes |
| step1.PNG | ![]() |
92452 bytes |
| step10.PNG | ![]() |
28558 bytes |
| step11.PNG | ![]() |
27660 bytes |
| step2.PNG | ![]() |
76860 bytes |
| step3.PNG | ![]() |
58506 bytes |
| step4.PNG | ![]() |
26826 bytes |
| step5.PNG | ![]() |
10954 bytes |
| step6.PNG | ![]() |
10602 bytes |
| step7.PNG | ![]() |
17703 bytes |
| step8.PNG | ![]() |
18958 bytes |
| step9.PNG | ![]() |
21010 bytes |