Skip to content

Instantly share code, notes, and snippets.

@aliencode
Last active December 23, 2015 18:59
Show Gist options
  • Select an option

  • Save aliencode/6679358 to your computer and use it in GitHub Desktop.

Select an option

Save aliencode/6679358 to your computer and use it in GitHub Desktop.
通过接口加载其实现子类

##java

package com.demo;
public abstract class Service1 {
    private static Service1 getInstance() {
    	return ServiceLoader.load(Service1.class).iterator().next();
    }
}

##配置

创建:

在目录/META-INF/services/建文件com.demo.Service1

内容:

com.demo.impl.Service1Impl1

com.demo.impl.Service1Impl2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment