7

According to HK2 @Service javadoc

Annotation placed on classes that are to be automatically added to an hk2 ServiceLocator.

I don't know how to make ServiceLocator find annotated classes automatically.

TestService

@Contract
public interface TestService {

}

TestServiceImpl

@Service
public class TestServiceImpl implements TestService {

}

Main

public static void main(String[] args) {
    ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();

    TestService service = locator.getService(TestServiceImpl.class);    
    System.out.println(service); // null
}

The result is always null. I have to add Descriptor so the ServiceLocator can find it.

public static void main(String[] args) {
    ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();

    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration config = dcs.createDynamicConfiguration();
    config.bind(BuilderHelper.link(TestServiceImpl.class).to(TestService.class).in(Singleton.class).build());
    config.commit();

    TestService service = locator.getService(TestServiceImpl.class);    
    System.out.println(service); // TestServiceImpl instance
}

How do I let ServiceLocator find the annotated classes automatically ? Did I misunderstand something ?

Rangi Lin
  • 9,303
  • 6
  • 45
  • 71

3 Answers3

5

You need to run the hk2-inhabitant-generator over your built classes in order to get automatic detection of services. There is more information here as well.

What that step does in the build process is to create a file named META-INF/hk2-locator/default with information about services. The createAndPopulateServiceLocator call then reads those files and automatically adds those service descriptors into the returned ServiceLocator.

jwells131313
  • 2,364
  • 1
  • 16
  • 26
  • 2
    As an update to this there is also now an easier to use hk2 metadata generator. All you have to do is put it in your classpath. More information here: https://hk2.java.net/2.4.0-b16/inhabitant-generator.html – jwells131313 Apr 08 '15 at 12:38
  • Not related to this answer in particular, but I am making some research regarding HK2 since a few days and everytime I find an answer the links to the real information/help always land on this start page https://javaee.github.io/hk2/ which is kind of not helpful :( – A4L Aug 15 '17 at 16:51
5

FYI, I was so frustrated with the reliance on the inhabitant files rather than having the capability for runtime scanning of annotated classes, I wrote this project:

https://github.com/VA-CTT/HK2Utilities

Since Eclipse / Maven / inhabitant runtime generators wouldn't play nice, it was nearly impossible to debug code that made use of HK2 in eclipse without runtime scanning.

The HK2Utilities package is available in central:

<dependency>
    <groupId>gov.va.oia</groupId>
    <artifactId>HK2Utilities</artifactId>
    <version>1.4.1</version>
</dependency>

To use it, you just call:

ServiceLocator locator = HK2RuntimeInitializer.init("myName", false, new String[]{"my.package.one", "my.package.two"});

This will scan the runtime classpath for classes in the packages listed, and automatically populate the service locator with them.

You don't ever have to generate inhabitant files with this model - and in practice, I found it to be faster performing than the inhabitant processing code as well (not that the performance matters much for this one-time operation)

---edit---

I still maintain this code - the current release is:

<dependency>
    <groupId>net.sagebits</groupId>
    <artifactId>HK2Utilities</artifactId>
    <version>1.5.2</version>
</dependency>

And the project location is now: https://github.com/darmbrust/HK2Utilities

user2163960
  • 1,871
  • 19
  • 22
  • Would like to be able to upvote multiple times. Thanks a lot for HK2Utilities! – zyexal Nov 05 '16 at 23:36
  • wow this is so gr8. Would you consider possibly contributing it to hk2 itself? – jwells131313 Aug 18 '17 at 15:52
  • 1
    I'd be happy to, but not sure if it was the sort of thing that was wanted in the light-weight HK2 package - it also pulls in another dependency that actually does the annotation scanning. It may be a bit until I find enough free cycles to put together a pull request. – user2163960 Aug 21 '17 at 15:12
0

Well now (2.6.1) all you need to do is add the dependencies - javax.inject, hk2-utils, hk2-api and hk2-metadata-generator.

When you build the project, javac compiler will generate a 'default' file in META-INF containing the wiring as follows:

[service-class-name]S
contract={contract-class-name}

This will be registered by the ServiceLocator during the run. This should be sufficient. However if that does not work, there are other options,

  • mvn plugin

    org.glassfish.hk2 hk2-inhabitant-generator 2.5.0-b36 generate-inhabitants

  • cmd line tool

    java org.jvnet.hk2.generator.HabitatGenerator [--file jarFileOrDirectory] [--outjar jarFile] [--locator locatorName] [--verbose]

More on this https://javaee.github.io/hk2/inhabitant-generator.html

arvindkgs
  • 373
  • 4
  • 12