首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用OSGi R6注解和吊索模型的AEM 6.3

使用OSGi R6注解和吊索模型的AEM 6.3
EN

Stack Overflow用户
提问于 2017-10-08 11:19:04
回答 2查看 3.1K关注 0票数 0

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

代码语言:javascript
复制
   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配置界面如下所示:

代码语言:javascript
复制
    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";
}

服务类如下所示:

代码语言:javascript
复制
   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;
    }
}

最后是服务接口:

代码语言:javascript
复制
package com.aem.sites.services;

public interface WeatherService {

    public String getName();

}

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

代码语言:javascript
复制
<sly data-sly-use.banner="com.aem.sites.models.Banner">

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

    </div>

</sly>

但是我看不到任何文本。我使用过logger.info,但在日志文件中也看不到它。我确信我在这里做了一些非常错误的事情,但是无法定位,因为我刚刚开始使用OSGi R6注释和吊索模型。任何帮助都是非常感谢的。

添加Maven依赖项:

父pom.xml

代码语言:javascript
复制
<!-- <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

代码语言:javascript
复制
<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>   
EN

回答 2

Stack Overflow用户

发布于 2017-10-08 14:19:41

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

因此,在WeatherServiceImpl更改中

代码语言:javascript
复制
 @Component(service=WeatherServiceImpl.class,

代码语言:javascript
复制
 @Component(service=WeatherService.class,

。。

:同样,configurationPolicy=ConfigurationPolicy.REQUIRE意味着为了激活DS组件,至少应该有一个配置。(代码中的配置不起作用)

因此,您可以转到系统/system/console/configMgr/com.aem.sites.services.impl.WeatherServiceImpl并输入一个值并保存。或者,您可以将configurationPolicy设置为optional,您的代码将在没有配置的情况下运行。

票数 1
EN

Stack Overflow用户

发布于 2017-10-09 14:30:14

为了使吊索型号可用,您必须正确配置maven-bundle-plugin<Sling-Model-Packages>部分,请参见https://sling.apache.org/documentation/bundles/models.html#basic-usage

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46627213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档