Spring中有ServiceLoader的类似物以及如何使用它?

2023-12-26

我想知道是否有 Spring 的类似物ServiceLoader类是标准 SDK API 的一部分。如果有这样的类怎么使用呢?

请指教!


Assume SpringFactoriesLoader是给你的。来自其 JavaDocs:

/*
 * General purpose factory loading mechanism for internal use within the framework.
 *
 * <p>The {@code SpringFactoriesLoader} loads and instantiates factories of a given type
 * from "META-INF/spring.factories" files. The file should be in {@link Properties} format,
 * where the key is the fully qualified interface or abstract class name, and the value
 * is a comma-separated list of implementation class names. For instance:
 *
 * <pre class="code">example.MyService=example.MyServiceImpl1,example.MyServiceImpl2</pre>
 *
 * where {@code MyService} is the name of the interface, and {@code MyServiceImpl1} and
 * {@code MyServiceImpl2} are the two implementations.
 */

我们的项目之一的样本:

META-INF/spring.factories:

org.springframework.integration.config.IntegrationConfigurationInitializer=\
org.springframework.integration.config.GlobalChannelInterceptorInitializer,\
org.springframework.integration.config.IntegrationConverterInitializer

实施:

public class IntegrationConfigurationBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Set<String> initializerNames = new HashSet<String>(
                SpringFactoriesLoader.loadFactoryNames(IntegrationConfigurationInitializer.class, beanFactory.getBeanClassLoader()));

        for (String initializerName : initializerNames) {
            try {
                Class<?> instanceClass = ClassUtils.forName(initializerName, beanFactory.getBeanClassLoader());
                Assert.isAssignable(IntegrationConfigurationInitializer.class, instanceClass);
                IntegrationConfigurationInitializer instance = (IntegrationConfigurationInitializer) instanceClass.newInstance();
                instance.initialize(beanFactory);
            }
            catch (Exception e) {
                throw new IllegalArgumentException("Cannot instantiate 'IntegrationConfigurationInitializer': " + initializerName, e);
            }
        }
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring中有ServiceLoader的类似物以及如何使用它? 的相关文章

  • Java中使用正则表达式确定字符串是否为URL [重复]

    这个问题在这里已经有答案了 可能的重复 检查字符串是否为有效 URL 的最佳正则表达式是什么 https stackoverflow com questions 161738 what is the best regular express
  • 点击 Java Web 服务:curl 或 URLConnection

    我使用的 Java 服务器在以下 URL 上公开 RESTful API http localhost 8080 my server 文档建议使用curl用于提交简单的PUT请求 文件上传 并强烈建议用户使用与示例中提供的完全相同的参数 所
  • 我们可以实例化一个抽象类吗?

    在一次采访中 有人问我 我们是否可以实例化一个抽象类 我的回答是 不 我们不能 但是 面试官告诉我 错了 我们可以 我对此争论了一下 然后他告诉我自己在家尝试一下 abstract class my public void mymethod
  • 行类型 Spark 数据集的编码器

    我想写一个编码器Row https spark apache org docs 2 0 0 api java index html org apache spark sql Row html输入 DataSet 用于我正在执行的地图操作 本
  • Java 7 中的 Beans Binding 将被什么取代?

    我在某处读到 我忘记了链接 Beans Binding 将不会成为 Java 7 的一部分 有人知道什么会取代它吗 另外 当前版本的 Java 中是否有 Bean 绑定的替代方案 我建议JGoodies 绑定 https binding d
  • 搜索 JTable 时 - 未获得正确的 ID

    所以我尝试在搜索名称后单击表 然后在其他表中编辑它 问题是我没有获得正确的 ID 而只获得第一个 ID JTable https i stack imgur com TnNIq png 搜索行动 https i stack imgur co
  • “错误:无法找到或加载主类 org.apache.hadoop.util.RunJar”是什么意思?

    我正在尝试运行一个示例 因为它指出 Hadoop 实践 一书 http www manning com lam 第 15 页 这是需要运行的命令 bin hadoop jar hadoop examples jar 但我收到这个错误 Err
  • Java 正则表达式中 \w 和 \b 的 Unicode 等效项?

    许多现代正则表达式实现解释 w字符类简写为 任何字母 数字或连接标点符号 通常 下划线 这样 正则表达式就像 w 匹配像这样的词hello l ve GO 432 or gefr ig 不幸的是 Java 没有 在爪哇 w仅限于 A Za
  • 如何在 JAVA servlet 中处理压缩 (gzip) HTTP 请求(不是响应) - 简单示例?

    我为这个问题苦苦挣扎了很长一段时间 在找到一个简单的解决方案后 想问一个问题和答案 这个问题在堆栈溢出时以不同的方式被多次提出 并且accepted solutions是partially correct and complex或谈论res
  • Spring REST 控制器返回带有空数据的 JSON [关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 我有一个简单的 Spring Boot Web 应用程序 我正在尝试从服务器接收一些数据 控制器返回一个集合 但浏览器收
  • 在 Java/Android 中查找 UTF-8 字符串中的字符数

    我试图找出字符串以 UTF 8 存储时的长度 我尝试了以下方法 String str Charset UTF8 CHARSET Charset forName UTF 8 byte abc str getBytes UTF8 CHARSET
  • Android-如何在指定时间后台下载数据

    我提前很抱歉没有发布任何代码 主要是因为我一生都无法弄清楚我需要如何做我需要做的事情 基本上 在一天中的指定时间间隔 例如下午 5 点 我希望我的应用程序从我的服务器下载一些数据并将其存储在设备上 这是为了减少每次运行应用程序时下载数据对我
  • Java - 在特定日期执行方法[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我需要在每年的特定日期执行一个方法 我该如何在java中执行此操作 Thanks Chris 按优先顺序排列 The Quartz htt
  • 使 @Schedule 在集群环境中仅运行一次

    我有两个 tomee 实例集群 每个都有一个方法注释如下 Schedule dayOfWeek public void runMeDaily 我只想每天运行一次这个方法 每天不两次 每个实例一次 我可以使用此处描述的标志仅在一个WebLog
  • JavaFX:在 WebView img 标签中未加载本地图像

    以下是我的代码 一切安好 我可以加载远程页面 我可以放置 HTML 内容 但我的img标签显示一个X标志表示无法加载图像 Note 我的图像与类位于同一个包中JavaFX在 Smiley 文件夹中 我可以列出所有图像 这意味着路径没有问题
  • 如何使用 Java 到 TestRail 的 API 将测试用例添加到现有测试运行中?

    我在执行期间创建了一个测试运行 我想在它们开始执行的同时添加测试用例 如果测试用例尚不存在 则已创建 并且该测试用例应该与其他测试用例一起添加到现有的测试运行中 我尝试过使用setCaseIds在运行期间和更新运行之后 但这会覆盖现有的运行
  • JFrame 类型的方法 ... 未定义

    我正在尝试制作一个带有两个菜单列表的 gui 每个菜单列表有 3 个项目 我的问题是 当我单击某个项目时 出现错误 JFrame 类型的方法 displayList int AirplaneList 未定义 AirplaneControll
  • 使用 Maven 将值附加到文件中

    我想在文件末尾附加一个值 但我无法确定要使用哪个插件 例子 我要附加的值 myValue file value1 value2 myValue 追加后 我知道我可以使用 antrun plugin 来做到这一点 但是可以使用 Maven 插
  • 数组所有可能的组合

    我有一个字符串数组 ted williams golden voice radio 我希望这些关键字的所有可能组合采用以下形式 ted williams golden voice radio ted williams ted golden
  • 为什么 JDOM 的 getChild() 方法返回 null?

    我正在做一个关于 html 文档操作的项目 我想要现有 html 文档中的正文内容将其修改为新的 html 现在我正在使用 JDOM 我想在我的编码中使用 body 元素 为此 我在编码中使用了 getChild body 但它向我的程序返

随机推荐