AEM에서 문자열 목록을 구성하고 여러 서비스에 걸쳐 공유해야합니다. 가장 좋은 방법은 무엇입니까? 목록은 런타임에 구성 가능해야합니다.AEM의 여러 OSGI 서비스에서 구성을 공유하는 방법
답변
구성한 하나 이상의 구성된 값이 필요한 다른 모든 OSGi 서비스에서 참조하는 전용 구성 서비스를 만들 수 있습니다.
예 구성 서비스
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
@Service(ConfigurationService.class)
@Component(immediate = true, metatype = true)
public class ConfigurationService {
@Property
private static final String CONF_VALUE1 = "configuration.value1";
private String value1;
@Property
private static final String CONF_VALUE2 = "configuration.value2";
private String value2;
@Activate
public void activate(final ComponentContext componentContext) {
this.value1 = PropertiesUtil.toString(componentContext.get(CONF_VALUE1), "");
this.value2 = PropertiesUtil.toString(componentContext.get(CONF_VALUE2), "");
}
public String getValue1() {
return this.value1;
}
public String getValue2() {
return this.value2;
}
}
이 이러한 클래스의 최소한이다. 하지만 Apache Felix Configuration Manager (/system/console/configMgr
)에서 구성 할 수있는 구성 가능한 OSGi 서비스가 만들어집니다.
참고 : @Component
주석에 metatype = true
을 사용하는 것이 중요합니다.
다음 단계는 "소비"서비스에서이 서비스를 참조하는 것입니다.
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.ComponentContext;
@Service(MyService.class)
@Component(immediate = true, metatype = true)
public class MyService {
@Reference
private ConfigurationService configurationService;
@Activate
public void activate(final ComponentContext componentContext) {
this.configurationService.getValue1();
}
}
참고 :이 예에서는 기본적으로 AEM과 함께 사용할 수있는 Apache SCR 주석을 사용합니다. SCR 주석이 예에서 사용에 대해 더 많은 것을 배울 수 있습니다 (@Service
는 @Component
는 @Reference
@Property
) 공식 문서에이 : Apache Felix SCR Annotation Documentation
예쁜 설명. 그러나, 우리는 여기서 인터페이스를 사용해야합니까? 클래스에서 구현하고 인터페이스를 서비스로 선언하는 것입니다. 그건 충분히 안전 해 보입니다. – theanubhava
@theanubhava 본문에서 언급했듯이 이것은 최소한의 것입니다. 분명히 여기서 개선 할 수있는 많은 것들이 있습니다. 간결함을 기하기 위해 나는 가능한 가장 짧은 예를 선택하여 기본 원리를 설명했다. 보통, 당신은 인터페이스를 사용하고'api'와'impl' 번들 등으로 나누었습니다. – Jens
이 본 적 있어요? http://www.nateyolles.com/blog/2015/10/updating-osgi-configurations-in-aem-and-sling. 쓰기 OSGI 구성을 읽기위한 OSGI 서비스. –