Service Adobe CQ:通过@Reference注入服务返回空值

Service Adobe CQ:通过@Reference注入服务返回空值,service,reference,dependency-injection,adobe,aem,Service,Reference,Dependency Injection,Adobe,Aem,我试图通过AdobeCQ5中的@Reference注释将服务注入组件,但在部署之后,它总是向我返回null,而不是服务实例 @Component(immediate = true) @Service(value = GoodbyeWorldService.class) public class GoodbyeWorldService { @Reference protected Scheduler scheduler; private final static Log

我试图通过AdobeCQ5中的@Reference注释将服务注入组件,但在部署之后,它总是向我返回null,而不是服务实例

@Component(immediate = true)
@Service(value = GoodbyeWorldService.class)  
public class GoodbyeWorldService {
    @Reference
    protected Scheduler scheduler;
    private final static Logger LOGGER = LoggerFactory.getLogger(GoodbyeWorldService.class);

    public void get() {
       LOGGER.info("Scheduler is " + this.scheduler);
   } 
}
JSP:

<%@ include file="/apps/cqblueprints-example/components/global.jspx" %>    
<jsp:directive.page contentType="text/html" pageEncoding="UTF-8"/>    
<jsp:useBean id="goodbye" class="com.cqblueprints.example.services.GoodbyeWorldService" scope="page" />      
<% goodbye.get(); %>    
我已经从这个bean测试了其他简单的打印方法。他们工作得很有魅力


我错过了什么

为了让OSGi注入
@Reference
字段,您不能将您的服务视为javabean。使用JSP文件中可用的
sling
对象以正确的方式获取服务:

<% GoodbyeWorldService service = sling.getService(GoodbyeWorldService.class); %>
<%= service.get() %>

完整示例:

<%@page session="false" import="your.package.GoodbyeWorldService"
%><%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"
%><sling:defineObjects/><%
    GoodbyeWorldService service = sling.getService(GoodbyeWorldService.class);
%><%= service.get() %>


问题解决了!我刚刚将maven scr插件的版本从1.7.4升级到1.9.0,它开始工作了!现在我通过@Reference注释检索我的实例,我在Felix OSGi控制台的“服务”和“组件”选项卡中看到我的服务。感谢所有在这里回复的人

请给我们看另一段代码摘录,其中您实际获得了GoodbyeWorldService并调用了get()方法。谢谢。我想你是对的!但不幸的是,我现在无法检查它,因为“sling.getService(GoodbyeWorldService.class);”将null作为服务返回给我,而不是实例本身。我认为这是因为我的服务不在OSGI控制台的“服务”选项卡中,但他的服务怎么可能发生呢?.services捆绑包在OSGI控制台中处于“活动”状态,服务中的方法正在正确调用,但“服务”选项卡中没有“GoodbeyWorldService”。早些时候它在那里,但现在它不在了。日志中没有任何错误。有什么想法吗,托梅克?谢谢您的回答。@user2620644您可以查看控制台中的“组件”选项卡。如果您的服务被正确地声明,它应该在那里,也许是停用的。尝试激活它,您可能会发现问题。@Tomek,不,它不在那里。“组件”选项卡中未列出我的服务。有什么想法吗?感谢您的回答。如果您的GoodbeyWorldService未处于活动状态,则可能是生成问题,如果使用maven scr插件生成,则应检查生成的bundle jar是否包含组件的xml文件,该文件位于OSGI-INF下,其中包含正确的scr:component定义。或者是捆绑依赖关系问题,您应该在error.log中看到。@BertrandDelacretaz,不,它不包括xml。lib文件夹(OSGI-INF/lib/…4个jar文件(库))中只有4个.jar文件,仅此而已。OSGI-INF文件夹中只有“lib”文件夹。我的maven bundle pom.xml正在执行scr目标:[INFO]——maven scr插件:1.7.4:scr(生成scr描述符)@cqblueprints示例服务--
<%@page session="false" import="your.package.GoodbyeWorldService"
%><%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"
%><sling:defineObjects/><%
    GoodbyeWorldService service = sling.getService(GoodbyeWorldService.class);
%><%= service.get() %>