2017-10-08 66 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