HomeBackend Development › OSGi services & Declarative Services

OSGi services & Declarative Services

Purpose: Write, wire and configure backend services with DS annotations — the dependency injection layer of AEM.

Who this page is for

AudienceWhy it matters to you
Backend engineersCore mechanics of every backend feature

Service in 20 lines

public interface PremiumCalculator { double monthly(String planId); }

@Component(service = PremiumCalculator.class)
@Designate(ocd = PremiumCalculatorImpl.Config.class)
public class PremiumCalculatorImpl implements PremiumCalculator {

    @ObjectClassDefinition(name = "PHI Premium Calculator")
    @interface Config {
        @AttributeDefinition(name = "Loading factor")
        double loadingFactor() default 1.0;
    }

    private double loadingFactor;

    @Activate @Modified
    void activate(Config cfg) { this.loadingFactor = cfg.loadingFactor(); }

    @Override public double monthly(String planId) { /* … */ return 0; }
}

Consume it anywhere: @Reference private PremiumCalculator calculator; (services) or @OSGiService (Sling Models).

Lifecycle & wiring

AnnotationMeaning
@ComponentRegister as DS component (service if service= given)
@Activate / @Deactivate / @ModifiedLifecycle callbacks; @Modified = config changed without restart
@ReferenceInject another service; policy/cardinality control optionality
@Designate + OCDTyped, console-visible configuration

Config values come from ui.config files named by PID (com.academy.phi.core.PremiumCalculatorImpl.cfg.json), per run mode. Live values visible in /system/console/configMgr.

Design rules

Quick navigation