且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java中可动态加载和卸载的应用程序模块-怎么样?

更新时间:2023-11-22 17:23:28

似乎OSGi正是您要的东西.它可能很复杂,但是有多种方法可以解决.通过使用SpringDM或类似的东西可以在运行时中处理注册和使用服务的样板任务,可以减轻某些复杂性.注释驱动的服务注册和依赖注入确实减少了需要编写的代码量.

It seems like OSGi is exactly what you're asking for. It can be complex, but there are ways to deal with that. Some of the complexity can be mitigated by using SpringDM or something similar to handle the boilerplate tasks of registering and consuming services in the runtime. Annotation-driven service registration and dependency injection really reduces the amount of code that needs to be written.

降低复杂性的另一种方法是将应用程序的大部分部署在单个捆绑包中,而仅将需要模块化的部分部署到自己的捆绑包中.这减少了您在运行时注册和使用其他捆绑软件中的服务的风险,并降低了部署的复杂性.与标准Java应用程序一样,在捆绑包中运行的代码可以使用同一捆绑包中的其他代码-无需与OSGi运行时进行交互.这种方法的反义是将您的应用程序分解为许多离散的捆绑包,这些捆绑包将定义明确的服务导出到系统中的其他捆绑包.尽管这是一种非常模块化的方法,但确实带来了额外的管理所有捆绑软件的复杂性以及与OSGi运行时的更多交互.

Another way to reduce complexity is to deploy the bulk of your application in a single bundle and only deploy the parts that need to be modular into their own bundles. This reduces your exposure to registering and using services from other bundles in the runtime as well as reducing the complexity of deployment. Code running within a bundle can use other code in the same bundle just as in a standard Java app - no need to interact with the OSGi runtime. The opposite of this approach is to break up your application into lots of discrete bundles that export well-defined services to other bundles in the system. While this is a very modular approach, it does come with extra complexity of managing all those bundles and more interaction with the OSGi runtime.

我建议您阅读《 OSGi in Action》一书,以了解这些问题并查看一些不错的示例.

I would suggest taking a look at the book "OSGi in Action" to get a sense of the issues and to see some decent samples.