【发布时间】:2018-05-18 16:20:36
【问题描述】:
我需要通过未注册为服务的普通 Java 类获取一些 OSGI 配置值,因此我不能使用 @Reference 或 @Inject 注释。我已经使用 Bundle 上下文来获取配置,但它正在工作。
public void getArticleName() {
final BundleContext bundleContext = FrameworkUtil.getBundle(ArticleNameService.class).getBundleContext();
try {
String articleName = (String) bundleContext.getService((bundleContext.getServiceReferences(ArticleNameService.class.getName(), " article.name "))[0]);
LOG.info("articleName......"+ articleName);
} catch (InvalidSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
服务类
@Service(ArticleNameService.class)
@Component(
metatype = true)
@Properties({
@Property(
name = "article.name", unbounded = PropertyUnbounded.ARRAY, cardinality = Integer.MAX_VALUE,
label = "article addrnameess"),
})
public class ArticleNameServiceImpl implements ArticleNameService
{
private static final String ARTICLE_NAME = "article.name";
private String[] articleName;
protected final void activate(final ComponentContext componentContext)
{
final Dictionary<String, Object> configurationProperties = componentContext.getProperties();
if (configurationProperties != null)
{
articleName = PropertiesUtil.toStringArray(configurationProperties.get(ARTICLE_NAME));
}
}
@Override
public final String[] getArticeName()
{
return articleName;
}
这是正确的做法吗?如果不是什么是正确的选择?
【问题讨论】:
-
我不明白你想达到什么目的。您想从配置管理员获取配置还是从服务获取服务属性?
-
我想在纯 java 中获得一个 cofig 值