Java的SPI引入Jar包簡單例子
阿新 • • 發佈:2020-07-14
我們把之前在一個專案中實現SPI機制的簡單例子(參見Java的SPI簡單例項),換成實際使用中的jar包引入的簡單例子。一拆為三,介面一個包,實現一個包,應用一個包,哦,還得加個骨架包。我們新增一個父專案:在IDEA中點選左上角File -> 點New Project -> 點Maven -> 點Next -> 輸入Maven的groupId和artifactId後點Next -> 輸入專案名後點Finish,只需在pom檔案中新增module:
<?xml version="1.0" encoding="UTF-8"?> <projectxmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wlf.spi</groupId> <artifactId>spi-parent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <java.version>1.8</java.version> </properties> <modules> <module>spi-api</module> <module>spi-impl</module> <module>spi-app</module> </modules> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
然後繼續依葫蘆畫瓢,分別新增如上三個子專案:介面spi-api,實現spi-impl和應用spi-app。為了使用介面,spi-impl和spi-app都依賴了spi-api包,spi-app載入實現類,還得引入spi-impl包。看看子專案的pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.wlf.spi</groupId> <artifactId>spi-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>spi-api</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> </project>
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.wlf.spi</groupId> <artifactId>spi-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>spi-app</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.wlf.spi</groupId> <artifactId>spi-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.wlf.spi</groupId> <artifactId>spi-impl</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.wlf.spi</groupId> <artifactId>spi-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>spi-impl</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.wlf.spi</groupId> <artifactId>spi-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/resources</directory> <targetPath>META-INF/</targetPath> </resource> </resources> </build> </project>
上面resource節點是為了把spi-impl包中的resources目錄下的內容在打jar包時,複製到META-INF目錄中,輸出META-INF/services/com.wlf.api.ITest這個檔案。這個檔案的內容執行實現類:
其他程式碼不變:
跑一把,結果跟單一專案中一樣: