[英]Configurable Java Servlet from OSGI
我正在尝试创建一个可通过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
}
}
为了能够处理此类注释,您需要设置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 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.