parent
a5922de701
commit
0f22307dc1
@ -0,0 +1,27 @@
|
|||||||
|
<?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">
|
||||||
|
<parent>
|
||||||
|
<artifactId>dubbo-project</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>DubboSPI接口</name>
|
||||||
|
|
||||||
|
<artifactId>dubbo-spi-api</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.dubbo</groupId>
|
||||||
|
<artifactId>dubbo</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.xjs.service;
|
||||||
|
|
||||||
|
import org.apache.dubbo.common.URL;
|
||||||
|
import org.apache.dubbo.common.extension.Adaptive;
|
||||||
|
import org.apache.dubbo.common.extension.SPI;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
@SPI("human")
|
||||||
|
public interface HelloService {
|
||||||
|
|
||||||
|
String sayHello();
|
||||||
|
|
||||||
|
@Adaptive
|
||||||
|
String sayHello(URL url);
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
<?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">
|
||||||
|
<parent>
|
||||||
|
<artifactId>dubbo-project</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>DubboSPI实现</name>
|
||||||
|
|
||||||
|
<artifactId>dubbo-spi-impl</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<artifactId>dubbo-spi-api</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.xjs.service.impl;
|
||||||
|
|
||||||
|
import com.xjs.service.HelloService;
|
||||||
|
import org.apache.dubbo.common.URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
public class DogHelloService implements HelloService {
|
||||||
|
@Override
|
||||||
|
public String sayHello() {
|
||||||
|
return "你在狗叫什么!";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String sayHello(URL url) {
|
||||||
|
return "wa wa url";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.xjs.service.impl;
|
||||||
|
|
||||||
|
import com.xjs.service.HelloService;
|
||||||
|
import org.apache.dubbo.common.URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
public class HumanHelloService implements HelloService {
|
||||||
|
@Override
|
||||||
|
public String sayHello() {
|
||||||
|
return "傻逼你好";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String sayHello(URL url) {
|
||||||
|
return "hello url";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
human=com.xjs.service.impl.HumanHelloService
|
||||||
|
dog=com.xjs.service.impl.DogHelloService
|
@ -0,0 +1,33 @@
|
|||||||
|
<?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">
|
||||||
|
<parent>
|
||||||
|
<artifactId>dubbo-project</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>DubboSPI调用</name>
|
||||||
|
|
||||||
|
<artifactId>dubbo-spi-main</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<artifactId>dubbo-spi-api</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<artifactId>dubbo-spi-impl</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import com.xjs.service.HelloService;
|
||||||
|
import org.apache.dubbo.common.URL;
|
||||||
|
import org.apache.dubbo.common.extension.ExtensionLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
public class DubboAdaptiveMain {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
URL url = URL.valueOf("test://localhost/hello?hello.service=dog");
|
||||||
|
|
||||||
|
HelloService helloService = ExtensionLoader.getExtensionLoader(HelloService.class).getAdaptiveExtension();
|
||||||
|
|
||||||
|
|
||||||
|
String s = helloService.sayHello(url);
|
||||||
|
|
||||||
|
System.out.println(s);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import com.xjs.service.HelloService;
|
||||||
|
import org.apache.dubbo.common.extension.ExtensionLoader;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
public class DubboSpiMain {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//获取扩展加载器
|
||||||
|
ExtensionLoader<HelloService> loader = ExtensionLoader.getExtensionLoader(HelloService.class);
|
||||||
|
|
||||||
|
//遍历所有支持的扩展点
|
||||||
|
Set<String> extensions = loader.getSupportedExtensions();
|
||||||
|
|
||||||
|
for (String extension : extensions) {
|
||||||
|
HelloService helloService = loader.getExtension(extension);
|
||||||
|
System.out.println(helloService.sayHello());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
<?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">
|
||||||
|
<parent>
|
||||||
|
<artifactId>xjs-study</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<name>JavaSPI机制</name>
|
||||||
|
<modules>
|
||||||
|
<module>spi-api</module>
|
||||||
|
<module>spi-impl</module>
|
||||||
|
<module>spi-main</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<artifactId>java-spi</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,21 @@
|
|||||||
|
<?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">
|
||||||
|
<parent>
|
||||||
|
<artifactId>java-spi</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<name>JavaSPI接口</name>
|
||||||
|
|
||||||
|
<artifactId>spi-api</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.xjs.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
public interface HelloService {
|
||||||
|
|
||||||
|
String sayHello();
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
<?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">
|
||||||
|
<parent>
|
||||||
|
<artifactId>java-spi</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>JavaSPI实现</name>
|
||||||
|
|
||||||
|
<artifactId>spi-impl</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<artifactId>spi-api</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.xjs.service.impl;
|
||||||
|
|
||||||
|
import com.xjs.service.HelloService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
public class DogHelloService implements HelloService {
|
||||||
|
@Override
|
||||||
|
public String sayHello() {
|
||||||
|
return "傻逼啊你";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.xjs.service.impl;
|
||||||
|
|
||||||
|
import com.xjs.service.HelloService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
public class HumanHelloService implements HelloService {
|
||||||
|
@Override
|
||||||
|
public String sayHello() {
|
||||||
|
return "Hello 你好!";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
com.xjs.service.impl.HumanHelloService
|
||||||
|
com.xjs.service.impl.DogHelloService
|
||||||
|
|
@ -0,0 +1,33 @@
|
|||||||
|
<?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">
|
||||||
|
<parent>
|
||||||
|
<artifactId>java-spi</artifactId>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<name>JavaSPI调用</name>
|
||||||
|
|
||||||
|
<artifactId>spi-main</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<artifactId>spi-api</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.xjs</groupId>
|
||||||
|
<artifactId>spi-impl</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.xjs;
|
||||||
|
|
||||||
|
import com.xjs.service.HelloService;
|
||||||
|
|
||||||
|
import java.util.ServiceLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-05-23
|
||||||
|
*/
|
||||||
|
public class JavaSpiMain {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final ServiceLoader<HelloService> helloServices = ServiceLoader.load(HelloService.class);
|
||||||
|
for (HelloService helloService : helloServices) {
|
||||||
|
System.out.println(helloService.getClass().getName()+":"+helloService.sayHello());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue