zoukankan      html  css  js  c++  java
  • Java的SPI引入Jar包简单例子

      我们把之前在一个项目中实现SPI机制的简单例子(参见Java的SPI简单实例 ),换成实际使用中的jar包引入的简单例子。一拆为三,接口一个包,实现一个包,应用一个包,哦,还得加个骨架包。我们新增一个父项目:在IDEA中点击左上角File -> 点New Project -> 点Maven -> 点Next -> 输入Maven的groupId和artifactId后点Next -> 输入项目名后点Finish,只需在pom文件中新增module:

    <?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>
    
        <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这个文件。这个文件的内容执行实现类:

      其他的接口类、实现类和应用类代码不变,包路径改改就好:

       跑一把,结果跟之前在单一项目跑的结果是一样的:

  • 相关阅读:
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Live2D 看板娘
    Bound mismatch: The typae CertificateDirectory is not a valid substitute for the bounded parameter <M extends Serializable>
    Duplicate property mapping of contactPhone found in
  • 原文地址:https://www.cnblogs.com/wuxun1997/p/13297958.html
Copyright © 2011-2022 走看看