Skip to content

Commit 6c5c149

Browse files
committed
java-spi
java service provider interface
1 parent dc0b1bc commit 6c5c149

File tree

13 files changed

+176
-0
lines changed

13 files changed

+176
-0
lines changed

java-spi/java-spi-api/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.hmkcode.api;
2+
3+
public interface MyService {
4+
5+
void doSomething();
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.hmkcode.api;
2+
3+
public interface MyServiceProviderInterface {
4+
5+
MyService getService();
6+
}
2.43 KB
Binary file not shown.
3.25 KB
Binary file not shown.

java-spi/java-spi-app/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}
2.43 KB
Binary file not shown.

java-spi/java-spi-impl1/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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>

0 commit comments

Comments
 (0)