【问题标题】:AEM 6.3 Using OSGi R6 Annotations and Sling ModelsAEM 6.3 使用 OSGi R6 注释和 Sling 模型
【发布时间】:2017-10-08 03:19:03
【问题描述】:

我正在尝试使用 OSGi R6 注释创建一个 OSGi 服务,然后将其注入到 Sling Model 类中,如下所示:

   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();

}

我正在尝试在 HTL 类中使用 Sling Model Pojo,如下所示:

<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>   

【问题讨论】:

  • 在您的模型中:您能否将@Inject 替换为@OSGiService?在官方文档中,@Inject 用于改编自SlingHttpServletRequest 的模型。您正在适应 Resource。这可能会有所不同,尽管我不是 100% 确定。

标签: java osgi aem sling htl


【解决方案1】:

您在服务实现中输入了service=WeatherServiceImpl.class,这是不正确的,它应该是服务接口名称。

所以,在 WeatherServiceImpl 中发生变化

 @Component(service=WeatherServiceImpl.class,

到

 @Component(service=WeatherService.class,

..

编辑:同样configurationPolicy=ConfigurationPolicy.REQUIRE 表示应该至少有一个配置才能使 DS 组件处于活动状态。 (代码中的配置不起作用)

所以您可以转到系统 /system/console/configMgr/com.aem.sites.services.impl.WeatherServiceImpl 并输入一个值并保存。或者您可以将 configurationPolicy 设置为可选,并且您的代码无需配置即可运行。

【讨论】:

  • 感谢您的回复。已经这样做了,但没有任何改变。
  • 我已经编辑了帖子以包含在两个 pom 文件中所做的更改。请看一看。谢谢。
  • 这很奇怪。我在 6.3 上试了试,能够一目了然地得到消息输出。我做了以下更改 - 1. 更改包名称以匹配我的沙盒设置。 2.用slf4j logger替换log4j logger
  • 我已对我的代码进行了少量修改。但这并不是说它有什么不同。你能检查一下一切是否仍然正常吗?我也实现了 slf4j
  • 我非常肯定此时的代码没有任何问题。我还假设您已经检查了系统控制台中的捆绑包是否处于活动状态,服务组件是否处于活动状态等。也许您可以将所有代码放在 github 上并共享链接。
【解决方案2】:

为了使 Sling 模型可用,您必须正确配置 maven-bundle-plugin 的 &lt;Sling-Model-Packages&gt; 部分,请参阅 https://sling.apache.org/documentation/bundles/models.html#basic-usage

您可以使用 Sling Models Web 控制台检查模型是否正确公开:http://localhost:4502/system/console/status-slingmodels

【讨论】:

  • 感谢您的回复。您是否认为 部分未正确配置在同一个包中,即 我有其他 sling 模型类,它们似乎工作正常。但这是一个没有按预期工作的类。我也尝试过使用 WCMUsePojo,但即使这样也没有用。所以,我不知道 pom.xml 是否有问题还是其他问题。
  • pom.xml 片段的最新更新看起来不错。我已验证,它在 AEM 6.3 上运行正常。我怀疑您没有正确包含 HTL 脚本,尝试在/apps/test/banner/banner.html 定义一个具有上述 sn-p 的最小 HTL 脚本,并在 /content/test 和 sling:resourceType=test/banner 定义一个节点,并看到它在 /content/test.html 正确呈现
  • 你能正确构建项目吗?我问这是因为我观察到 Maven 构建的一些问题。 stackoverflow.com/questions/46659511/…
  • 我将您的类和脚本复制粘贴到一个工作项目中
猜你喜欢
  • 2018-04-06
  • 1970-01-01
  • 2018-03-13
  • 1970-01-01
  • 2020-08-28
  • 2018-06-13
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
相关资源
最近更新 更多