【问题标题】:Configurable Java Servlet from OSGI来自 OSGI 的可配置 Java Servlet
【发布时间】:2013-03-29 21:19:34
【问题描述】:

我正在尝试创建一个可通过 OSGi 控制台进行配置的 Java 类。我听说你可以通过 SCR 注释来做到这一点,但不完全确定如何。我已经掌握了大部分内容,但不确定要获取和发布什么以及如何在 JSP 中引用它。这是我目前所拥有的:

import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import javax.servlet.ServletException;
import java.io.IOException;

@SlingServlet(
paths={"/somepath/"}
)
@Properties({
@Property(name="email.add", value="Email Info",propertyPrivate=false),
@Property(name="user.info",value="User Info", propertyPrivate=false)
})
public class WelcomeMessage extends SlingAllMethodsServlet
{
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse                                 response) throws ServletException, IOException
{
    //Do something here
}

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
    //Do something here
}
}

【问题讨论】:

  • 这是 sling 特定的,需要 sling osgi 包才能工作。您还需要通过 scr maven 插件处理注释。如果您不需要 sling,请查看 Apache Http Servlet 桥以助您一臂之力。
  • 感谢您的回复。抱歉,但我很困惑我不确定你通过 maven 插件处理注释是什么意思你有一个简单的例子吗?我已经安装了 Maven,但不确定处理注释是什么意思。抱歉,完整,我的意思是完整的新手。

标签: java sling osgi-bundle


【解决方案1】:

为了能够处理此类注释,您需要设置 Maven SCR 插件(来自 Apache Felix)。该插件将处理注释并在生成的 JAR 文件中创建元数据。

@SlingServlet 注释是特定于 Apache Sling 的,需要特定的 Apache Sling 包才能注册 servlet。 @SlingServlet 注解也由 Maven SCR 插件处理。

这是一个关于如何在 Maven 中配置 SCR 插件的示例。

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.felix</groupId>
      <artifactId>maven-scr-plugin</artifactId>
      <version>1.9.0</version>
      <executions>
        <execution>
          <id>generate-scr-scrdescriptor</id>
          <goals>
            <goal>scr</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

此外,为了能够创建 OSGi 包(带有 OSGi 元数据的 Jar),您需要设置 Maven 包插件。

您可以在此处找到有关 Maven SCR 插件的简短文档:http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin.html

Maven Bundle 插件文档在这里:http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html

但是,了解这一点的最佳方法是查看 Sling 捆绑包中的示例:https://github.com/apache/sling/tree/trunk/bundles

【讨论】:

  • 感谢@stenrs,这很有帮助。绝对为我指明了正确的方向。再次感谢!
猜你喜欢
  • 2014-12-22
  • 2022-10-24
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 2011-07-06
  • 1970-01-01
相关资源
最近更新 更多