OSGi 的首选模型是在服务注册表中注册一个 Servlet 服务。该 servlet 应该由安装在该框架上的 Http 服务器获取。该模型将在不久的将来成为标准模型,但已被 Apache Felix 支持。这就是您在 bnd(tools) 中进行此操作的方式:
bnd.bnd
-runfw: org.apache.felix.framework;version='[4,5)'
-runbundles: \
org.apache.felix.configadmin; version=1.6.0, \
org.apache.felix.log; version=1.0.1, \
org.apache.felix.scr; version=1.6.0, \
org.apache.felix.http.jetty; version=2.2.0, \
org.apache.felix.http.whiteboard; version=2.2.0
如果它正在运行,您可以使用这样的声明式服务编写一个 servlet:
@Component(provide=Servlet.class,properties="alias=/hello") // makes it available on /hello
public class MyAndroidServer extends HttpServlet {
public void doGet(HttpServletRequest rq, HttpSerletResponse rsp) throws IOException {
rsp.getWriter().println("Hello World");
}
}
如果你从 bndtools 开始,它应该很容易让它工作。据我所知,这是在 OSGi 环境中使用 servlet 的最简单方法。