2014-09-22 49 views
0

CQ5的QueryBuilder参考我声明吊索的servlet像这样在吊带的Servlet

@Component(metatype = false) 
@Service(Servlet.class) 
@Properties({ 
     @Property(name = "sling.servlet.paths", value = "/bin/foo/bar"), 
     @Property(name = "sling.servlet.methods", value = "POST") }) 
public class FooBarServlet extends SlingAllMethodsServlet { 
    ... 
} 

我重写的doPost像这样

@Override 
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { 
    ... 
} 

,我能够从客户端发布。大!

我扔在以下

@Reference 
private QueryBuilder queryBuilder; 

按文档,参考查询建设者应注射。但似乎没有。在日志中我看到这个错误

bindQueryBuilder cannot be found (java.lang.VerifyError: ... 

当我尝试发布到Servlet我得到这个

javax.jcr.RepositoryException: org.apache.sling.api.resource.PersistenceException: Resource at '/bin/foo/bar' is not modifiable. 

而在OSGi控制台我看到的是安装在我的包,这是什么不得不说我的小服务器

Service ID 3075 Types: javax.servlet.Servlet 
Service PID: com.myproject.FooBarServlet 
Component Name: com.myproject.FooBarServlet 
Component ID: 5526 
Vendor: Adobe 

任何关于我在做什么的错误?

回答

2

我一直在使用this教程作为参考。 我碰到this来对菲利克斯服务组件运行时(SCR)

,所以我采取了以下

protected void activate(ComponentContext context) { 
    LOGGER.info("activating {}", this.getClass().getName()); 
} 

protected void unbindQueryBuilder(QueryBuilder queryBuilder) { 
    this.queryBuilder = null; 
} 

protected void bindQueryBuilder(QueryBuilder queryBuilder) { 
    this.queryBuilder = queryBuilder; 
} 

和它的工作!所以,经过仔细调查中,我了解到,这些绑定/解除绑定方法实际上应该由Maven的SCR-插件生成,其中我有1.6.0版本

  <plugin> 
       <groupId>org.apache.felix</groupId> 
       <artifactId>maven-scr-plugin</artifactId> 
       <version>1.6.0</version> 
       <executions> 
        <execution> 
         <id>generate-scr-scrdescriptor</id> 
         <goals> 
          <goal>scr</goal> 
         </goals> 
         <configuration> 
          <!-- Private service properties for all services. --> 
          <properties> 
           <service.vendor>Adobe</service.vendor> 
          </properties> 
         </configuration> 
        </execution> 
       </executions> 
       <dependencies> 
        <dependency> 
         <groupId>org.slf4j</groupId> 
         <artifactId>slf4j-simple</artifactId> 
         <version>1.5.2</version> 
        </dependency> 
       </dependencies> 
      </plugin> 

和我有1.4.0注释

  <dependency> 
       <groupId>org.apache.felix</groupId> 
       <artifactId>org.apache.felix.scr.annotations</artifactId> 
       <version>1.4.0</version> 
       <scope>provided</scope> 
      </dependency> 

所以虽然我不知道为什么没有得到生成绑定/解除绑定的方法,我知道他们是应该的,所以我手动生成它们。

更新 我尝试更新的maven-SCR-插件1.20.0版本,MVN期间产生了以下错误打造

[ERROR] Project depends on org.apache.felix:org.apache.felix.scr.annotations:jar:1.4.0:provided 
[ERROR] Minimum required version is 1.9.0 

所以...我更新了org.apache。 felix.scr.annotations为1.9.0。它的工作原理!我的绑定/取消绑定访问器生成,并且都很棒。然而,我担心并不知道我是否应该使用org.apache.felix.scr.annotations的1.9.0版本,因为我在maven依赖项中将它标记为provided,并且当我查看安装在cq上的OSGi捆绑包时实例我看到以下内容

Apache Felix Declarative Services (org.apache.felix.scr) : Version 1.6.3.R1409029 
+0

尝试使用更近[插件]的版本(http://mvnrepository.com/artifact/org.apache.felix/maven-scr-plugin/1.20.0 )和[注释](http://mvnrepository.com/artifact/org.apache.felix/org.apache.felix.scr.annotations/1.9.8)。方法应该自动创建。 – 2014-09-23 06:29:00

+0

@TomekRękawek,我更新了依赖关系并进行了更新。 – 2014-09-23 14:08:29

+0

伙计们,你们发现问题的原因了吗? – gstackoverflow 2014-09-23 17:51:43

-4

对于依赖注入的工作,您应该声明成员变量为public。

尝试将其更改为

@Reference 
public QueryBuilder queryBuilder; 
+1

我不同意,你不应该公开成员。对于servlet,您需要将其设置为“私有瞬态”。 – Thomas 2014-09-23 08:19:28

+0

我试过了,它不起作用。最重要的不是它在文档和教程中的写法。 – 2014-09-23 13:35:27

+0

你在doPost()方法中放置@Reference事物吗? – 2014-09-24 09:26:30