【发布时间】:2016-12-04 23:30:50
【问题描述】:
我从here 下载了 fscontext.jar 文件并将其放入我的类路径中。
这是我的代码:
import javax.naming.*;
import java.io.File;
public class JNDIHello {
final static String URL =
"file:///Users/Koray Tugay/Development/studentform/src/main/java/biz/tugay";
final static String CONTEXT_FACTORY
= "com.sun.jndi.fscontext.RefFSContextFactory";
static {
System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
System.setProperty(Context.PROVIDER_URL, URL);
}
public static void main(String[] args) throws Exception {
final InitialContext context = new InitialContext();
final File file = (File) context.lookup("TestClass.java");
System.out.println("File exists? " + file.exists());
}
}
输出将是:
File exists? true
这很棒。我正在使用 RefFSContextFactory 和 JNDI 从上下文中获取 File objects。
我想要做的是能够获得 DataSource 对象,就像在 Web 应用程序中一样。一个例子可以看here。
我已经在本地运行了h2db,它的属性如下:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/studentform" auth="Container" type="javax.sql.DataSource"
maxActive="5" maxIdle="10" maxWait="10000"
username="sa" password="" driverClassName="org.h2.Driver"
url="jdbc:h2:tcp://localhost:9092/~/h2dbs/studentform"/>
</Context>
但是我应该使用什么 jndi Factory 实现以及如何在独立的 Java 应用程序中定义 URL?
【问题讨论】:
标签: java datasource jndi