JerseyInjection

From APIDesign

(Difference between revisions)
Jump to: navigation, search
(Injection)
(The Real Example)
Line 49: Line 49:
== The Real Example ==
== The Real Example ==
 +
 +
Here is [https://github.com/jtulach/timing/commit/e5be764bb61c1c251d3d795eb517f559f25fddfc the diff] that [[I]] applied to my [https://github.com/jtulach/timing real project] to enable the use of [[injection]].
 +
 +
And here is the change that allowed me to pre-configure the instance of '''Storage''' when initializing the server: [https://github.com/jtulach/timing/commit/9e1e7398f9c26b9ca3245e50a09bf8427ccf43de the diff].
 +
 +
== The Best Tutorial ==
 +
 +
Of course, all of this is already explained in an [https://jersey.java.net/documentation/latest/ioc.html existing tutorial] - it is just not that easy to find it. It really helps if a guy who wrote significant part of [[Jersey]] is your co-worker, sitting 2m away from you...
 +
 +
Thanks Jakub!

Revision as of 05:17, 15 March 2017

I am using Jersey for a long time to implement REST services and I am relatively happy Jersey user. One thing that is problematic is the fact that I am using Jersey as a standalone application (without any J2EE container) and finding tutorials how to do it isn't always easy. For example how to use ComponentInjection with such standalone Jersey was a mystery for me for a long time...

Contents

Injection

Of course, the trivial things are easy. One can use the javax.inject annotations to identify the injection slots:

@Path("/timing/") @Singleton
public final class TimingResource {
    @Inject
    private Storage storage;
    @Inject
    private ContactsResource contacts;
}

and when preparing the application register classes implementing these components into the system:

static HttpServer createServer(URI u, File dir) {
   ResourceConfig rc = new ResourceConfig(
       TimingResource.class, ContactsResource.class, Storage.class, Main.class
   );
   HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u, rc);
}

that works, but it has one problem: how can I configure the instances? The system just calls their default (no argument) constructor and for example the Storage class would benefit from actually knowing the location where to store its data. So far I was solving that with a system property: Reading System.getProperty("storage.dir") inside the default constructor. That is of course quite hacky - I knew there had to be a better way, but I never invested enough time to find it out. Just today...

Your Own Binder

The solution is simple. One can register own AbstractBinder:

ResourceConfig rc = new ResourceConfig(
    TimingResource.class, ContactsResource.class, Main.class
);
final Storage storage = dir == null ? Storage.empty() : Storage.forDirectory(dir);
rc.registerInstances(new AbstractBinder() {
    @Override
    protected void configure() {
        bind(storage).to(Storage.class);
    }
});
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u, rc);

Now it is possible to properly prepare and configure the appropriate instance of Storage and then just bind it to Storage.class when the Injection is about to happen. There is just a single downside: this is a Jersey specific solution - it uses org.glassfish.hk2.utilities.binding.AbstractBinder - but yeah, great that it works and the non JAX-RS standardized code is anyway located just in the main method initialization.

The Real Example

Here is the diff that I applied to my real project to enable the use of injection.

And here is the change that allowed me to pre-configure the instance of Storage when initializing the server: the diff.

The Best Tutorial

Of course, all of this is already explained in an existing tutorial - it is just not that easy to find it. It really helps if a guy who wrote significant part of Jersey is your co-worker, sitting 2m away from you...

Thanks Jakub!

Personal tools
buy