Servlets OSGi声明性服务注入
我试图在本地Glassfish服务器上使用声明性服务来实现一个简单的OSGi服务。提供的插件始终处于活动状态 我在向使用我的服务的servlet中注入时遇到了问题,当调用servlet时,引用为null,因为它与注入服务引用的对象不同 我通过在引用设置器中放置断点来测试它,我看到我的服务被注入,但是当我单击将servlet调用到应用程序中的按钮时,服务引用为null,因为它不是同一个对象(即,在servlet#u实例#1中注入,但在servlet#u实例#2上调用该方法。我必须缺少一点细节,因为我可以在执行时找到并使用我的服务Servlets OSGi声明性服务注入,servlets,dependency-injection,osgi,declarative-services,maven-scr-plugin,Servlets,Dependency Injection,Osgi,Declarative Services,Maven Scr Plugin,我试图在本地Glassfish服务器上使用声明性服务来实现一个简单的OSGi服务。提供的插件始终处于活动状态 我在向使用我的服务的servlet中注入时遇到了问题,当调用servlet时,引用为null,因为它与注入服务引用的对象不同 我通过在引用设置器中放置断点来测试它,我看到我的服务被注入,但是当我单击将servlet调用到应用程序中的按钮时,服务引用为null,因为它不是同一个对象(即,在servlet#u实例#1中注入,但在servlet#u实例#2上调用该方法。我必须缺少一点细节,因为
final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
loggingTestServiceInterface = (LoggingTestServiceInterface) bundleContext.getService(bundleContext
.getServiceReference(LoggingTestServiceInterface.class.getName()));
用于生成我的XMLs文件的插件:maven scr插件
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.14.0</version>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>war</supportedProjectType>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
</supportedProjectTypes>
</configuration>
</plugin>
生成的XML
<?xml version="1.0" encoding="UTF-8"?>
<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<scr:component name="com.sti.sigbud.servlet.Wakarimashita" activate="start" deactivate="stop" modified="modify">
<implementation class="com.sti.sigbud.servlet.Wakarimashita"/>
<reference name="LoggingTestServiceInterface" interface="com.sti.loggingservices.serviceinterface.LoggingTestServiceInterface" cardinality="1..1" policy="dynamic" bind="bindLoggingTestServiceInterface" unbind="unbindLoggingTestServiceInterface"/>
</scr:component>
生成的XML是
<?xml version="1.0" encoding="UTF-8"?>
<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<scr:component enabled="true" immediate="false" name="Shikashi" activate="start" deactivate="stop" modified="modify">
<implementation class="com.sti.logging.service.LoggingTestService"/>
<service servicefactory="false">
<provide interface="com.sti.loggingservices.serviceinterface.LoggingTestServiceInterface"/>
</service>
</scr:component>
<?xml version="1.0" encoding="UTF-8"?>
<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<scr:component name="com.sti.sigbud.servlet.Wakarimashita" activate="start" deactivate="stop" modified="modify">
<implementation class="com.sti.sigbud.servlet.Wakarimashita"/>
<service servicefactory="false">
<provide interface="javax.servlet.Servlet"/>
</service>
<property name="alias" value="/Wakarimashita"/>
<reference name="LoggingTestServiceInterface" interface="com.sti.loggingservices.serviceinterface.LoggingTestServiceInterface" cardinality="1..1" policy="dynamic" bind="bindLoggingTestServiceInterface" unbind="unbindLoggingTestServiceInterface"/>
</scr:component>
我从JSP访问servlet
<form action="Wakarimashita" method="GET">
<input type="text" name="language" size="50"/>
<input type="submit" value="Submit" />
</form>
为了测试以上内容,我在部署的bundle org.apache.felix.http.api-2.2.1、org.apache.felix.http.whiteboard-2.2.1中使用了与文章中相同的包,但没有找到是否需要打开开关
我还检查了org.apache.felix.webconsole-4.2.0-所有捆绑包,服务已经启动并运行,它说我的消费者捆绑包正在使用它。您有两方创建servlet实例。一方是DS,另一方是web容器。您不能有两个主机。web容器基本上必须负责since它将只向它创建的servlet实例发送请求 如果有一个同时支持web容器和DS的实现,那么您就可以了。但是我从来没有听说过这样的事情
我不知道Glassfish是否支持OSGi Web应用程序规范(第128章)。如果是这样,那么您可以与OSGi服务层进行交互,如128.6所述。Wakarimasen deshita…您好,我发现我面临两个容器来管理我的代码。因为我似乎使用Glassfish OSGi Web容器,所以我可以通过getServletContext().getAttributes(“OSGi bundle”)访问我的捆绑包上下文就像在规范中一样,从那里获取我的服务,我想知道Glassfish中是否有配置或插件/工具或任何其他方式来创建我的servlet,让我使用injectionNope,不是/就是。好吧,我将按照规范访问服务,因此使用getServletContext().getAttributes从servlet上下文访问服务(“OSGi束”在这种情况下获取我的服务和服务引用,因为您不能使用DS,请考虑使用适当地管理服务的使用。
@Component(service = Servlet.class, property = {"alias=/Wakarimashita"})
public class Wakarimashita extends HttpServlet
<?xml version="1.0" encoding="UTF-8"?>
<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
<scr:component name="com.sti.sigbud.servlet.Wakarimashita" activate="start" deactivate="stop" modified="modify">
<implementation class="com.sti.sigbud.servlet.Wakarimashita"/>
<service servicefactory="false">
<provide interface="javax.servlet.Servlet"/>
</service>
<property name="alias" value="/Wakarimashita"/>
<reference name="LoggingTestServiceInterface" interface="com.sti.loggingservices.serviceinterface.LoggingTestServiceInterface" cardinality="1..1" policy="dynamic" bind="bindLoggingTestServiceInterface" unbind="unbindLoggingTestServiceInterface"/>
</scr:component>
<form action="Wakarimashita" method="GET">
<input type="text" name="language" size="50"/>
<input type="submit" value="Submit" />
</form>