May 1, 2020
Estimated Post Reading Time ~

Codes snippet for Declaring OSGi Service

@Component(
label = "Service name",
description = "Service description",
metatype = true,
immediate = false)
@Properties({
    @Property(
            label = "My Label"
            name = "property-name",
            value = "my value",
            propertyPrivate = true
    )
})
@Service
public class SampleServiceImpl implements SampleService {
    private final Logger log = 
    LoggerFactory.getLogger(this.getClass());

    /**
     * OSGi Properties *
     */
    private static final String DEFAULT_SAMPLE = "hello!";
    private String mySample = DEFAULT_SAMPLE;
    @Property(label = "Prop name", 
      description = "Prop description", 
      value = DEFAULT_SAMPLE)
    public static final String PROP_SAMPLE = "sample";

    ...
   
    @Activate
    protected void activate(final Map<String, String> config) { 
        mySample = PropertiesUtil.toString(config.get(PROP_SAMPLE), DEFAULT_SAMPLE);
    }

    @Deactivate
    protected void deactivate(final Map<String, String> config) { }
}


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.