OSGi服務
瞭解OSGi服務開發的基本概念,包括:
- 如何將Java POJO轉換為OSGi服務
- 如何將OSGi服務繫結至Java介面
資源
程式碼
Activities.java
/core/src/main/java/com/adobe/aem/wknd/examples/core/adventures/Activities.java
package com.adobe.aem.wknd.examples.core.adventures;
import org.osgi.annotation.versioning.ProviderType;
@ProviderType
public interface Activities {
String getRandomActivity();
}
ActivitiesImpl.java
/core/src/main/java/com/adobe/aem/wknd/examples/core/adventures/impl/ActivitiesImpl.java
package com.adobe.aem.wknd.examples.core.adventures.impl;
import java.util.Random;
import com.adobe.aem.wknd.examples.core.adventures.Activities;
import org.osgi.service.component.annotations.Component;
@Component(
service = { Activities.class }
)
public class ActivitiesImpl implements Activities {
private static final String[] ACTIVITIES = new String[] {
"Camping", "Skiing", "Skateboarding"
};
//private final int randomIndex = new Random().nextInt(ACTIVITIES.length);
private final Random random = new Random();
/**
* @return the name of a random WKND adventure activity
*/
public String getRandomActivity() {
int randomIndex = random.nextInt(ACTIVITIES.length);
return ACTIVITIES[randomIndex];
}
}
package-info.java
/core/src/main/java/com/adobe/aem/wknd/examples/core/adventures/package-info.java
@Version("1.0")
package com.adobe.aem.wknd.examples.core.adventures;
import org.osgi.annotation.versioning.Version;
需要新增package-info.java,以確保AEM中的其他OSGi套件組合可以解析OSGi服務介面(或任何Java類別)。 如果package-info.java遺失,將不會匯出Java套件及其Java介面或類別。 其他OSGi套件組合嘗試從這個Java套件匯入這些Java介面或類別時,將會出現錯誤訊息無法解析 (在AEM的OSGi套件組合主控台中)。
recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69