OSGi seems to be exactly what you are asking for. It can be tricky, but there are ways to handle it. Some complexity can be mitigated using SpringDM or something similar to handle the template tasks of registering and using services at runtime. Annotated service registration and dependency injection really reduce the amount of code that needs to be written.
Another way to reduce complexity is to deploy the main part of your application in one bundle and only deploy the parts, which must be modular, into your own packages. This reduces the likelihood of registering and using services from other packages at run time, and also reduces deployment complexity. The code running inside the package may use different code in the same package as in the standard Java application - there is no need to interact with the OSGi runtime. The opposite of this approach is to split your application into many discrete packages that export well-defined services to other packages in the system. Although this is a very modular approach, it comes with the added complexity of managing all of these packages and great interaction with the OSGi runtime.
I would suggest taking a look at the OSGi in Action book to sort out the problems and see some decent examples.
source share