【问题标题】:@Activate not called when starting component启动组件时未调用@Activate
【发布时间】:2018-02-03 11:35:53
【问题描述】:

Liferay's documentation about making modules configurable 之后,我编写了这个 Liferay 7 模块:

@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");
    }
}

当我部署时,日志只显示:

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

为什么没有调用activate 方法?

在 Gogo Shell 中重启模块也不会调用activate

【问题讨论】:

    标签: liferay osgi liferay-7


    【解决方案1】:

    默认情况下,声明式服务中的组件仅在其服务被另一个捆绑包引用时才会激活。

    如果您希望它立即启动,请使用 immediate=true

    【讨论】:

    • 通常你不应该使用immediate=true。也许您想这样做只是为了向自己证明该组件正在工作,但请考虑该组件是否真的总是需要急切地实例化,即使没有人愿意使用它提供的服务。
    • 另外,在组件被激活之前,也不能调用构造函数。所以我不明白为什么调用了构造函数但没有调用 activate 方法。看起来像是 SCR 中的错​​误?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    • 2012-02-08
    • 1970-01-01
    • 2018-09-21
    • 2015-04-17
    相关资源
    最近更新 更多