[英]AEM 6.3 Using OSGi R6 Annotations and Sling Models
I am trying to create an OSGi Service using OSGi R6 annotations and then injecting it in the Sling Model class like this: 我试图使用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;
}
}
The OSGi configuration interface looks like this: 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";
}
and the service class looks like this: 服务类如下所示:
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;
}
}
and finally the service interface: 最后是服务接口:
package com.aem.sites.services;
public interface WeatherService {
public String getName();
}
I am trying to use the Sling Model Pojo in the HTL class like this: 我试图像这样在HTL类中使用Sling Model Pojo:
<sly data-sly-use.banner="com.aem.sites.models.Banner">
<div class="inner">
<h2>${banner.message}</h2
</div>
</sly>
Adding Maven dependencies: 添加Maven依赖项:
Parent pom.xml 父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>
Core pom.xml 核心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>
In the service impl you have put service=WeatherServiceImpl.class
which is incorrect, it should be the service interface name. 在服务提示中,您输入了service=WeatherServiceImpl.class
,这是不正确的,应该是服务接口名称。
so, in WeatherServiceImpl change 因此,在WeatherServiceImpl中进行更改
@Component(service=WeatherServiceImpl.class,
to 至
@Component(service=WeatherService.class,
.. ..
EDIT : also configurationPolicy=ConfigurationPolicy.REQUIRE
means that there should be at least one config in order for the DS component to be active. 编辑 :还configurationPolicy=ConfigurationPolicy.REQUIRE
意味着应该至少有一个配置,以便DS组件处于活动状态。 (config from code wont work) (从代码配置将无法正常工作)
so you can go to system /system/console/configMgr/com.aem.sites.services.impl.WeatherServiceImpl and put a value and save. 因此,您可以转到系统/system/console/configMgr/com.aem.sites.services.impl.WeatherServiceImpl并输入值并保存。 or you can set configurationPolicy to optional and your code will work without config. 或者,您可以将configurationPolicy设置为可选,并且代码无需配置即可工作。
In order to make the Sling Models available you have to properly configure <Sling-Model-Packages>
section of the maven-bundle-plugin
, see https://sling.apache.org/documentation/bundles/models.html#basic-usage 为了使Sling模型可用,您必须正确配置maven-bundle-plugin
<Sling-Model-Packages>
部分,请参阅https://sling.apache.org/documentation/bundles/models.html#basic-用法
You can check the models are properly exposed by using the Sling Models Web Console: http://localhost:4502/system/console/status-slingmodels 您可以使用Sling Models Web控制台检查模型是否正确公开: http:// localhost:4502 / system / console / status-slingmodels
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.