2017-10-08 64 views
0

我試圖使用OSGi R6註釋來創建OSGi服務,然後注入其在吊帶Model類是這樣的:AEM 6.3使用OSGi R6註解和吊帶模式

package com.aem.sites.models; 

import javax.annotation.PostConstruct; 
import javax.inject.Inject; 

import org.apache.sling.api.resource.Resource; 
import org.apache.sling.models.annotations.Model; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

import com.aem.sites.services.WeatherService; 

@Model(adaptables=Resource.class) 
public class Banner { 

    final static Logger logger = LoggerFactory.getLogger(Banner.class); 

    @Inject 
    private WeatherService weatherService; 

    private String message; 

     @PostConstruct 
     public void init() { 
      logger.info("##############################################################calling the init method"); 
      message = weatherService.getName(); 
     } 

     public String getMessage() { 
      logger.info("##############################################################inside the get message method"); 
      return message; 
     } 

} 

OSGi的配置界面看起來是這樣的:

package com.aem.sites.interfaces; 

import org.osgi.service.metatype.annotations.AttributeDefinition; 
import org.osgi.service.metatype.annotations.AttributeType; 
import org.osgi.service.metatype.annotations.ObjectClassDefinition; 

@ObjectClassDefinition(name = "Configuration", description = "Configuration file") 
public @interface Configuration { 

    @AttributeDefinition(
       name = "String Property", 
       description = "Sample String property", 
       type = AttributeType.STRING 
      ) 
    public String getText() default "It's a test"; 
} 

和服務類看起來是這樣的:

package com.aem.sites.services.impl; 


import org.osgi.service.component.annotations.Activate; 
import org.osgi.service.component.annotations.Component; 
import org.osgi.service.component.annotations.Deactivate; 
import org.osgi.service.component.annotations.Modified; 
import org.osgi.service.component.annotations.ConfigurationPolicy; 
import org.osgi.service.metatype.annotations.Designate; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 


import com.aem.sites.interfaces.Configuration; 
import com.aem.sites.services.WeatherService; 

@Component(service=WeatherService.class, 
immediate=true, 
configurationPid = "com.aem.sites.services.impl.WeatherServiceImpl", 
configurationPolicy=ConfigurationPolicy.REQUIRE 
) 
@Designate(ocd = Configuration.class) 
public class WeatherServiceImpl implements WeatherService{ 

    final static Logger logger =LoggerFactory.getLogger(WeatherServiceImpl.class); 

    private Configuration config; 

    private String name; 

    @Activate 
    @Modified 
    protected final void activate(Configuration configuration) { 
     logger.info("##############################################################calling activate method inside the weather service impl class"); 
     this.config = configuration; 
     name = config.getText(); 
    } 

    @Deactivate 
    protected void deactivate() { 
    } 

    @Override 
    public String getName() { 
     logger.info("##############################################################calling get name method inside the weather service impl class"); 
     return name; 
    } 
} 

和鰭加盟服務接口:

package com.aem.sites.services; 

public interface WeatherService { 

    public String getName(); 

} 

我試圖用吊帶POJO的模型在HTL類是這樣的:

<sly data-sly-use.banner="com.aem.sites.models.Banner"> 

    <div class="inner"> 
     <h2>${banner.message}</h2 

    </div> 

</sly> 

但我無法看到任何文本。我使用了logger.info,但無法在日誌文件中看到它。我確信我在這裏做了一些非常錯誤的事情,但是因爲我剛剛開始使用OSGi R6註釋和Sling模型進行遊戲,所以無法找到它。任何幫助表示讚賞。

添加Maven依賴:

父pom.xml的

<!-- <plugin> 
        <groupId>org.apache.felix</groupId> 
        <artifactId>maven-bundle-plugin</artifactId> 
        <version>2.5.3</version> 
       </plugin> -->    
       <plugin> 
         <groupId>org.apache.felix</groupId> 
         <artifactId>maven-bundle-plugin</artifactId> 
         <version>3.3.0</version> 
         <inherited>true</inherited> 
       </plugin> 

<dependency> 
       <groupId>org.osgi</groupId> 
       <artifactId>org.osgi.service.component.annotations</artifactId> 
       <version>1.3.0</version> 
       <scope>compile</scope> 
      </dependency> 

      <dependency> 
       <groupId>org.osgi</groupId> 
       <artifactId>org.osgi.annotation</artifactId> 
       <version>6.0.0</version> 
      </dependency> 

      <dependency> 
       <groupId>org.osgi</groupId> 
       <artifactId>org.osgi.service.metatype.annotations</artifactId> 
       <version>1.3.0</version> 
      </dependency> 

核心的pom.xml

<dependency> 
       <groupId>org.osgi</groupId> 
       <artifactId>org.osgi.service.component.annotations</artifactId> 
      </dependency> 
      <dependency> 
       <groupId>org.osgi</groupId> 
       <artifactId>org.osgi.annotation</artifactId> 
      </dependency> 
      <dependency> 
       <groupId>org.osgi</groupId> 
       <artifactId>org.osgi.service.metatype.annotations</artifactId> 
      </dependency> 

<plugin> 
       <groupId>org.apache.felix</groupId> 
       <artifactId>maven-bundle-plugin</artifactId> 
       <extensions>true</extensions> 
       <configuration> 
        <instructions> 
         <Bundle-SymbolicName>com.aem.site.aem-site</Bundle-SymbolicName> 
         <Sling-Model-Packages> 
          com.aem.sites.models 
         </Sling-Model-Packages> 
         <Import-Package>javax.inject;version=0.0.0,*</Import-Package> 
        </instructions> 
       </configuration> 
      </plugin> 
+0

在你的模型:你可以用'@ OSGiService'替換'@Inject'嗎?在官方文檔中,'@ Inject'用於從SlingHttpServletRequest改編的模型。您正在從「資源」調整。這可能會有所作爲,雖然我不是100%確定。 – Jens

回答

1

在IMPL你已經把service=WeatherServiceImpl.class這是不正確的服務,它應該是服務接口名稱。

所以,在WeatherServiceImpl改變

@Component(service=WeatherServiceImpl.class, 

@Component(service=WeatherService.class, 

..

EDIT:還configurationPolicy=ConfigurationPolicy.REQUIRE意味着應該有至少一個配置爲使DS組件被激活。 (配置代碼不會工作)

所以你可以去系統/system/console/configMgr/com.aem.sites.services.impl.WeatherServiceImpl並把一個值並保存。或者您可以將configurationPolicy設置爲可選,並且您的代碼將在沒有配置的情況下工作。

+1

謝謝你的迴應。已經做到了,但沒有改變。 – user972418

+0

我已經編輯過這篇文章,以包含在這兩個pom文件中所做的更改。請看一看。謝謝。 – user972418

+0

這很奇怪。我嘗試了6.3,並能夠很容易地獲得消息輸出。我做了以下更改 - 1.更改軟件包名稱以匹配我的沙箱設置。 2.用slf4j記錄器替換log4j記錄器 – awd

0

爲了使吊帶可選型號,你必須正確地配置maven-bundle-plugin<Sling-Model-Packages>節,看到https://sling.apache.org/documentation/bundles/models.html#basic-usage

您可以檢查模型是否正確使用吊索模型Web控制檯暴露:http://localhost:4502/system/console/status-slingmodels

+0

感謝您的回覆。你認爲部分沒有正確配置在同一個包中,即我有其他的吊索模型類,他們似乎工作正常。但是這是一個不能按預期工作的課程。我也試過使用WCMUsePojo,但即使這樣也行不通。所以,我不知道pom.xml是否有一些問題或是其他問題。 – user972418

+0

「pom.xml」片段的最新更新看起來沒問題。我驗證了它在AEM 6.3上正常工作。我懷疑你沒有正確地包含HTL腳本,試着用's/apps/test/banner/banner.html'和'/ content/test'處的'sling'來定義一個最小化的HTL腳本, resourceType = test/banner',並在'/ content/test.html'處看到它正確顯示。 – Vlad

+0

您是否能夠正確構建項目?我問這是因爲我觀察到一些Maven構建的問題。 https://stackoverflow.com/questions/46659511/aem-6-3-maven-build-is-successful-but-still-doesnt-install-all-the-content – user972418