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";
}
添加回答
举报