http://wiki.apidesign.org/index.php?title=JerseyInjection&feed=atom&action=historyJerseyInjection - Revision history2024-03-29T00:17:56ZRevision history for this page on the wikiMediaWiki 1.12.0rc1http://wiki.apidesign.org/index.php?title=JerseyInjection&diff=9071&oldid=prevJaroslavTulach: /* The Real Example */2017-03-15T05:25:55Z<p><span class="autocomment">The Real Example</span></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr>
<td colspan='2' style="background-color: white; color:black;">←Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 05:25, 15 March 2017</td>
</tr>
<tr><td colspan="2" class="diff-lineno">Line 50:</td>
<td colspan="2" class="diff-lineno">Line 50:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== The Real Example ==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== The Real Example ==</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>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]].</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>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 <ins style="color: red; font-weight: bold; text-decoration: none;">you can find </ins>the change that <ins style="color: red; font-weight: bold; text-decoration: none;">allows </ins>me to pre-configure the instance of '''Storage''' when initializing the server: [https://github.com/jtulach/timing/commit/9e1e7398f9c26b9ca3245e50a09bf8427ccf43de the diff].</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div> </div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>And here <del style="color: red; font-weight: bold; text-decoration: none;">is </del>the change that <del style="color: red; font-weight: bold; text-decoration: none;">allowed </del>me to pre-configure the instance of '''Storage''' when initializing the server: [https://github.com/jtulach/timing/commit/9e1e7398f9c26b9ca3245e50a09bf8427ccf43de the diff].</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== The Best Tutorial ==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== The Best Tutorial ==</div></td></tr>
</table>JaroslavTulachhttp://wiki.apidesign.org/index.php?title=JerseyInjection&diff=9069&oldid=prevJaroslavTulach: /* Injection */2017-03-15T05:22:40Z<p><span class="autocomment">Injection</span></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr>
<td colspan='2' style="background-color: white; color:black;">←Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 05:22, 15 March 2017</td>
</tr>
<tr><td colspan="2" class="diff-lineno">Line 26:</td>
<td colspan="2" class="diff-lineno">Line 26:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div></source></div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div></source></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;">that </del>works, but it has one problem: how can <del style="color: red; font-weight: bold; text-decoration: none;">I configure </del>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...</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">Such solution </ins>works, but it has one problem: how can the instances <ins style="color: red; font-weight: bold; text-decoration: none;">be configured</ins>? 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...</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Your Own Binder ==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Your Own Binder ==</div></td></tr>
</table>JaroslavTulachhttp://wiki.apidesign.org/index.php?title=JerseyInjection&diff=9068&oldid=prevJaroslavTulach: /* Injection */2017-03-15T05:21:53Z<p><span class="autocomment">Injection</span></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr>
<td colspan='2' style="background-color: white; color:black;">←Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 05:21, 15 March 2017</td>
</tr>
<tr><td colspan="2" class="diff-lineno">Line 15:</td>
<td colspan="2" class="diff-lineno">Line 15:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div></source></div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div></source></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;">and when preparing </del>the <del style="color: red; font-weight: bold; text-decoration: none;">application </del>register classes implementing these components into the system:</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">That is one side of </ins>the <ins style="color: red; font-weight: bold; text-decoration: none;">story. The other side requires one to </ins>register classes implementing these components into the system <ins style="color: red; font-weight: bold; text-decoration: none;">when configuring the server</ins>:</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><source lang="java"></div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><source lang="java"></div></td></tr>
</table>JaroslavTulachhttp://wiki.apidesign.org/index.php?title=JerseyInjection&diff=9067&oldid=prevJaroslavTulach at 05:17, 15 March 20172017-03-15T05:17:56Z<p></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr>
<td colspan='2' style="background-color: white; color:black;">←Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 05:17, 15 March 2017</td>
</tr>
<tr><td colspan="2" class="diff-lineno">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div>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 [[<del style="color: red; font-weight: bold; text-decoration: none;">ComponentInjection</del>]] with such standalone [[Jersey]] was a mystery for me for a long time...</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>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 [[<ins style="color: red; font-weight: bold; text-decoration: none;">Component Injection</ins>]] with such standalone [[Jersey]] was a mystery for me for a long time...</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Injection ==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Injection ==</div></td></tr>
</table>JaroslavTulachhttp://wiki.apidesign.org/index.php?title=JerseyInjection&diff=9066&oldid=prevJaroslavTulach: /* The Real Example */2017-03-15T05:17:15Z<p><span class="autocomment">The Real Example</span></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr>
<td colspan='2' style="background-color: white; color:black;">←Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 05:17, 15 March 2017</td>
</tr>
<tr><td colspan="2" class="diff-lineno">Line 49:</td>
<td colspan="2" class="diff-lineno">Line 49:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== The Real Example ==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== The Real Example ==</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">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]].</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">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].</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">== The Best Tutorial ==</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">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...</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">Thanks Jakub!</ins></div></td></tr>
</table>JaroslavTulachhttp://wiki.apidesign.org/index.php?title=JerseyInjection&diff=9065&oldid=prevJaroslavTulach: /* Injection */2017-03-15T05:10:11Z<p><span class="autocomment">Injection</span></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr>
<td colspan='2' style="background-color: white; color:black;">←Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 05:10, 15 March 2017</td>
</tr>
<tr><td colspan="2" class="diff-lineno">Line 18:</td>
<td colspan="2" class="diff-lineno">Line 18:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><source lang="java"></div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><source lang="java"></div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>static HttpServer createServer(URI u, File dir) {</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>static HttpServer createServer(URI u, File dir) {</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>ResourceConfig rc = new ResourceConfig(</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"> </ins>ResourceConfig rc = new ResourceConfig(</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>TimingResource.class, ContactsResource.class, Storage.class, Main.class</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"> </ins>TimingResource.class, ContactsResource.class, Storage.class, Main.class</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>);</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"> </ins>);</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u, rc);</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"> </ins>HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u, rc);</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> return server;</del></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>}</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>}</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div></source></div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div></source></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
</table>JaroslavTulachhttp://wiki.apidesign.org/index.php?title=JerseyInjection&diff=9064&oldid=prevJaroslavTulach: /* Your Own Binder */2017-03-15T05:09:34Z<p><span class="autocomment">Your Own Binder</span></p>
<table style="background-color: white; color:black;">
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr>
<td colspan='2' style="background-color: white; color:black;">←Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 05:09, 15 March 2017</td>
</tr>
<tr><td colspan="2" class="diff-lineno">Line 34:</td>
<td colspan="2" class="diff-lineno">Line 34:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><source lang="java"></div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><source lang="java"></div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>ResourceConfig rc = new ResourceConfig(</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>ResourceConfig rc = new ResourceConfig(</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>TimingResource.class, ContactsResource.class, Main.class</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"> </ins>TimingResource.class, ContactsResource.class, Main.class</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>);</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>);</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>final Storage storage = dir == null ? Storage.empty() : Storage.forDirectory(dir);</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>final Storage storage = dir == null ? Storage.empty() : Storage.forDirectory(dir);</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>rc.registerInstances(new AbstractBinder() {</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>rc.registerInstances(new AbstractBinder() {</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>@Override</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"> </ins>@Override</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>protected void configure() {</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"> </ins>protected void configure() {</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>bind(storage).to(Storage.class);</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"> </ins>bind(storage).to(Storage.class);</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>}</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"> </ins>}</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>});</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>});</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> </del>HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u, rc)<del style="color: red; font-weight: bold; text-decoration: none;">;</del></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u, rc);</div></td></tr>
<tr><td class='diff-marker'>-</td><td style="background: #ffa; color:black; font-size: smaller;"><div><del style="color: red; font-weight: bold; text-decoration: none;"> return server</del>;</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div></source> </div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div></source> </div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
</table>JaroslavTulachhttp://wiki.apidesign.org/index.php?title=JerseyInjection&diff=9063&oldid=prevJaroslavTulach: New page: 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...2017-03-15T05:08:24Z<p>New page: I am using <a href="/wiki/Jersey" title="Jersey">Jersey</a> for a long time to implement <a href="/wiki/REST" title="REST">REST</a> services and <a href="/wiki/I" class="mw-redirect" title="I">I</a> am relatively happy <a href="/wiki/Jersey" title="Jersey">Jersey</a> user. One thing that is problematic is the fact that <a href="/wiki/I" class="mw-redirect" title="I">I</a> am using <a href="/wiki/Jersey" title="Jersey">Jersey</a>...</p>
<p><b>New page</b></p><div>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...<br />
<br />
== Injection ==<br />
<br />
Of course, the trivial things are easy. One can use the '''javax.inject''' annotations to identify the injection slots:<br />
<br />
<source lang="java"><br />
@Path("/timing/") @Singleton<br />
public final class TimingResource {<br />
@Inject<br />
private Storage storage;<br />
@Inject<br />
private ContactsResource contacts;<br />
}<br />
</source><br />
<br />
and when preparing the application register classes implementing these components into the system:<br />
<br />
<source lang="java"><br />
static HttpServer createServer(URI u, File dir) {<br />
ResourceConfig rc = new ResourceConfig(<br />
TimingResource.class, ContactsResource.class, Storage.class, Main.class<br />
);<br />
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u, rc);<br />
return server;<br />
}<br />
</source><br />
<br />
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...<br />
<br />
== Your Own Binder ==<br />
<br />
The solution is simple. One can register own '''AbstractBinder''':<br />
<br />
<source lang="java"><br />
ResourceConfig rc = new ResourceConfig(<br />
TimingResource.class, ContactsResource.class, Main.class<br />
);<br />
final Storage storage = dir == null ? Storage.empty() : Storage.forDirectory(dir);<br />
rc.registerInstances(new AbstractBinder() {<br />
@Override<br />
protected void configure() {<br />
bind(storage).to(Storage.class);<br />
}<br />
});<br />
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(u, rc);<br />
return server;<br />
</source> <br />
<br />
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.<br />
<br />
== The Real Example ==</div>JaroslavTulach