Changes between Version 2 and Version 3 of Java/ServiceProvider


Ignore:
Timestamp:
05/05/2011 09:49:04 (6 years ago)
Author:
dumoulin
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Java/ServiceProvider

    v2 v3  
    55Although this system is efficient, editing plain text files in META-INF/services/ could be a pain because they are not checked during the compilation. Netbeans offers a solution by introducing the annotation @ServiceProvider. 
    66 
    7 == @ServiceProvider and maven == 
     7== @ServiceProvider and maven 3 == 
    88 
    99To be able to use this annotation and to ensure its process during compilation, you have to: 
    10  * use a recent version of the maven compiler, by putting something like that in your pom: 
     10 * add the netbeans repository to your project: 
     11{{{ 
     12        <repository> 
     13            <id>netbeans</id> 
     14            <url>http://bits.netbeans.org/maven2/</url> 
     15        </repository> 
     16}}} 
     17 * add the lookup as dependency of your project: 
     18{{{ 
     19        <dependency> 
     20            <groupId>org.netbeans.api</groupId> 
     21            <artifactId>org-openide-util-lookup</artifactId> 
     22            <version>RELEASE70</version> 
     23        </dependency> 
     24}}} 
     25 * then you can use the annotation in your code, and check that the files are correctly created in your jar during packaging phase: 
     26{{{ 
     27@ServiceProvider(service=MyService.class) 
     28public class MyImplementation { 
     29}}} 
     30* for retrieving registered services, you should use org.openide.util. Lookup utility class: 
     31{{{ 
     32Lookup.getDefault().lookupResult(MyService.class).allClasses() 
     33}}} 
     34or 
     35{{{ 
     36Lookup.getDefault().lookupResult(MyService.class).allInstances() 
     37}}} 
     38 
     39When I was using maven in version 2.x, I should specify the version of the compiler by putting something like that in your pom: 
    1140{{{ 
    1241    <build> 
     
    2655    </build> 
    2756}}} 
    28  * add the lookup as dependency of your project: 
    29 {{{ 
    30         <dependency> 
    31             <groupId>org.netbeans.api</groupId> 
    32             <artifactId>org-openide-util-lookup</artifactId> 
    33             <version>RELEASE69</version> 
    34         </dependency> 
    35 }}} 
    36  * then you can use the annotation in your code, and check that the files are correctly created in your jar during packaging phase: 
    37 {{{ 
    38 @ServiceProvider(service=MyService.class) 
    39 public class MyImplementation { 
    40 }}} 
    41 * for retrieving registered services, you should use org.openide.util. Lookup utility class: 
    42 {{{ 
    43 Lookup.getDefault().lookupResult(MyService.class).allClasses() 
    44 }}} 
    45 or 
    46 {{{ 
    47 Lookup.getDefault().lookupResult(MyService.class).allInstances() 
    48 }}}