亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 aem groovy 控制臺中獲取服務?

如何在 aem groovy 控制臺中獲取服務?

哈士奇WWW 2023-06-14 15:43:14
我需要創建一個 Java 服務,并從 aem groovy 控制臺請求該服務。我創建了一個作為 Java 類的服務,構建并部署到本地 aem 實例,當我嘗試使用 getService 函數詢問它時,它不起作用,并且出現錯誤。我正在嘗試以這種方式獲得服務:def jcrService = getService(TestService.class)out.println(jcrService.test())并以這種方式:import example.core.services.TestService;TestService testService = new TestService();out.println(testService.test())當我導入我的服務類時,它正在工作。但是當我實現與 jcr.Session 和 jcr.Nodes 一起工作的基本接口時,它不起作用。
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

提供 FQCN(完全合格的類名)對我來說很好。請找到以下示例。


常規腳本:


def testService = getService("com.project.services.TestService")


println "title is: " + testService.getTitle();

print "url is: " + testService.getUrl();

結果:


title is: Test Service Title

url is: http://localhost:4502

測試服務:


package com.project.services;


public interface TestService {

    public String getTitle();

    public String getUrl();

}

測試服務實現


package com.project.services.impl;


import org.osgi.service.component.annotations.Activate;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.metatype.annotations.Designate;


import com.project.services.configuration.TestServiceConfig;

import com.project.services.TestService;


@Component(service = TestService.class, name = "TestService", immediate = true)

@Designate(ocd = TestServiceConfig.class)

public class TestServiceImpl implements TestService {


    private String url;


    @Override

    public String getTitle() {

        return "Test Service Title";

    }


    @Override

    public String getUrl() {

        return url;

    }


    @Activate

    protected void activate(TestServiceConfig testServiceConfig) {

        url = testServiceConfig.url();

    }


}


測試服務配置


package com.project.services.configuration;


import org.osgi.service.metatype.annotations.AttributeDefinition;

import org.osgi.service.metatype.annotations.ObjectClassDefinition;


@ObjectClassDefinition(name = "Test Service Config", description = "Test Service Configuration.")

public @interface TestServiceConfig {


    @AttributeDefinition(name = "url", description = "Provide URL for localhost")

    String url() default "http://localhost:4502";

}


查看完整回答
反對 回復 2023-06-14
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號