3

Following Liferay's documentation about making modules configurable, I wrote this Liferay 7 module:

@Component(configurationPid = "myproject.api.TranslationConfiguration")
public class TranslationServiceImpl implements TranslationService {
    private volatile TranslationConfiguration configuration;

    public TranslationServiceImpl() {
        log.info("TranslationServiceImpl constructor");
    }

    @Activate
    @Modified
    protected void activate(Map<String, Object> properties) {
        log.info("Hello from activate");
    }
}

When I deploy, the log only shows:

TranslationServiceImpl constructor
Service registered.
STARTED myproject.impl_1.0.0 [538]

Why is the activate method not called?

Restarting the module in Gogo Shell does not call activate either.

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373

1 Answers1

4

By default a component in declarative services is only activated when its service is referred by another bundle.

If you want it to start right away use immediate=true

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • Normally you should not use `immediate=true`. Maybe you want to do this just to prove to yourself that the component is working, but think about whether this component really always needs to be eagerly instantiated even when nobody wants to use the service it provides. – Neil Bartlett Aug 25 '17 at 16:26
  • 1
    Also, until the component is activated, the constructor must not be called either. So I do not see why the constructor was called but the activate method was not. Seems like a bug in SCR? – BJ Hargrave Aug 25 '17 at 17:14