在OSGi环境中融合JNDI与Java EE的深入解析
1. OSGi环境下的初始上下文建立
在OSGi环境中,访问初始上下文(Initial - Context)需要不同的机制,即使用OSGi服务。以下是具体的实现步骤:
1. 获取JNDIContextManager服务 :
ServiceReference ref =
bundleContext.getServiceReference(
JNDIContextManager.class.toString());
JNDIContextManager ctxtMgr =
(JNDIContextManager) bundleContext.getService(ref);
- 设置属性 :如同在Java EE中一样,需要设置
INITIAL_CONTEXT_FACTORY
属性,指定要使用的JNDI提供者。
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
- 创建初始上下文 :调用