gpt4 book ai didi

jakarta-ee - 如何向 Java Web 应用程序添加模块化功能

转载 作者:行者123 更新时间:2023-12-02 08:11:26 24 4
gpt4 key购买 nike

我开发了一个基于 Java EE 的 Web 应用程序 Open Source Project

现在有一些贡献者like to add additional functionality通过插入网络应用程序的模块。

您能否解释一下如何实现这一目标或指导我引用源代码。

最佳答案

我想说 OSGi 是你应该考虑的东西。我没有该领域的专业知识,但您可以在 stackoverflow 和其他在线资源中找到示例和描述。

What does OSGi solve?

Trouble understanding the whole OSGi web eco system

Introduction to OSGi教程使用Apache FelixApache Karaf并提供了有关创建服务包的看似简单的教程。来自教程:

The Open Service Gateway Initiative is a specification defining a Java-based component system. It’s currently managed by the OSGi Alliance, and its first version dates back to 1999. Since then, it has proved to be a great standard for component systems, and it’s widely used nowadays. The Eclipse IDE, for instance, is an OSGi-based application.

看似更复杂的教程可能是 OSGi Modularity - Tutorial

ServiceLoader 提供一个简单的服务提供商加载工具。但看起来可以根据您的需求进行简化。它适用于 spring-boot,但似乎不适用于企业应用程序。一个简单的例子如下:

您的应用程序的框架:

public class FrameworkClass {    
public static void main(String[] args) {
new FrameworkClass().run();
}
private void run() {
ServiceLoader<IFrameworkModule> modules = ServiceLoader.load(IFrameworkModule.class);
modules.forEach(IFrameworkModule::initialize);
modules.forEach(IFrameworkModule::execute);
}
}

服务模块实现的接口(interface):

public interface IFrameworkModule {
public void initialize();
public void execute();
}

一个模块 - 在一个单独的 jar 中 - 用于应用程序

public class Module1 implements IFrameworkModule {
@Override
public void initialize() {
System.out.println("initialize module1");
}

@Override
public void execute() {
System.out.println("execute module1");
}
}

需要 META-INF/services 文件夹中的 framework.IFrameworkModule 文件

fmodule.Module1

但考虑到应用程序的复杂性,我认为使用 OSGi 更有意义。

关于jakarta-ee - 如何向 Java Web 应用程序添加模块化功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57123801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com