2014-09-22 44 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