File tree Expand file tree Collapse file tree 13 files changed +176
-0
lines changed
src/main/java/com/hmkcode/api
src/main/java/com/hmkcode/app
resources/META-INF/services Expand file tree Collapse file tree 13 files changed +176
-0
lines changed Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+
3+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <modelVersion >4.0.0</modelVersion >
6+
7+ <groupId >com.hmkcode.api</groupId >
8+ <artifactId >java-spi-api</artifactId >
9+ <version >1.0-SNAPSHOT</version >
10+
11+ <name >java-spi-api</name >
12+ <!-- FIXME change it to the project's website -->
13+ <url >http://www.example.com</url >
14+
15+ <properties >
16+ <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
17+ <maven .compiler.source>1.7</maven .compiler.source>
18+ <maven .compiler.target>1.7</maven .compiler.target>
19+ </properties >
20+
21+
22+ <dependencies >
23+
24+ </dependencies >
25+
26+
27+ </project >
Original file line number Diff line number Diff line change 1+ package com .hmkcode .api ;
2+
3+ public interface MyService {
4+
5+ void doSomething ();
6+ }
Original file line number Diff line number Diff line change 1+ package com .hmkcode .api ;
2+
3+ public interface MyServiceProviderInterface {
4+
5+ MyService getService ();
6+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+
3+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <modelVersion >4.0.0</modelVersion >
6+
7+ <groupId >com.hmkcode.app</groupId >
8+ <artifactId >java-spi-app</artifactId >
9+ <version >1.0-SNAPSHOT</version >
10+
11+ <name >java-spi-app</name >
12+ <!-- FIXME change it to the project's website -->
13+ <url >http://www.example.com</url >
14+
15+ <properties >
16+ <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
17+ <maven .compiler.source>1.7</maven .compiler.source>
18+ <maven .compiler.target>1.7</maven .compiler.target>
19+ </properties >
20+
21+
22+ <dependencies >
23+ <dependency >
24+ <groupId >com.hmkcode.api</groupId >
25+ <artifactId >java-spi-api</artifactId >
26+ <version >1.0-SNAPSHOT</version >
27+ <scope >system</scope >
28+ <systemPath >${project.basedir} /lib/java-spi-api-1.0-SNAPSHOT.jar</systemPath >
29+ </dependency >
30+ <dependency >
31+ <groupId >com.hmkcode.impl</groupId >
32+ <artifactId >java-spi-impl1</artifactId >
33+ <version >1.0-SNAPSHOT</version >
34+ <scope >system</scope >
35+ <systemPath >${project.basedir} /lib/java-spi-impl1-1.0-SNAPSHOT.jar</systemPath >
36+ </dependency >
37+ </dependencies >
38+
39+
40+ </project >
Original file line number Diff line number Diff line change 1+ package com .hmkcode .app ;
2+
3+ public class App
4+ {
5+ public static void main ( String [] args )
6+ {
7+ MyServiceLoader .defaultProvider ().getService ().doSomething ();
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ package com .hmkcode .app ;
2+
3+ import java .nio .file .ProviderNotFoundException ;
4+ import java .util .Iterator ;
5+ import java .util .ServiceLoader ;
6+
7+ import com .hmkcode .api .MyServiceProviderInterface ;
8+
9+
10+ public class MyServiceLoader {
11+
12+ private static final String DEFAULT_PROVIDER = "com.hmkcode.impl.MyServiceProviderImpl1" ;
13+
14+ public static MyServiceProviderInterface defaultProvider () {
15+ return provider (DEFAULT_PROVIDER );
16+ }
17+
18+ public static MyServiceProviderInterface provider (String providerName ) {
19+ ServiceLoader <MyServiceProviderInterface > loader = ServiceLoader .load (MyServiceProviderInterface .class );
20+
21+ Iterator <MyServiceProviderInterface > it = loader .iterator ();
22+ while (it .hasNext ()) {
23+ MyServiceProviderInterface provider = it .next ();
24+ if (providerName .equals (provider .getClass ().getName ())) {
25+ return provider ;
26+ }
27+ }
28+ throw new ProviderNotFoundException ("provider " + providerName + " not found" );
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+
3+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <modelVersion >4.0.0</modelVersion >
6+
7+ <groupId >com.hmkcode.impl</groupId >
8+ <artifactId >java-spi-impl1</artifactId >
9+ <version >1.0-SNAPSHOT</version >
10+
11+ <name >java-spi-impl1</name >
12+ <!-- FIXME change it to the project's website -->
13+ <url >http://www.example.com</url >
14+
15+ <properties >
16+ <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
17+ <maven .compiler.source>1.7</maven .compiler.source>
18+ <maven .compiler.target>1.7</maven .compiler.target>
19+ </properties >
20+
21+ <dependencies >
22+ <dependency >
23+ <groupId >com.hmkcode.api</groupId >
24+ <artifactId >java-spi-api</artifactId >
25+ <version >1.0-SNAPSHOT</version >
26+ <scope >system</scope >
27+ <systemPath >${project.basedir} /lib/java-spi-api-1.0-SNAPSHOT.jar</systemPath >
28+ </dependency >
29+ </dependencies >
30+
31+ </project >
You can’t perform that action at this time.
0 commit comments