我在使用 Spring 设置 JNDI 配置时遇到问题。我检查了其他帖子,但无法解决我的问题。我使用 Tomcat 6 作为容器。根据我的理解,我需要在服务器上设置资源。所以在我的server.xml
文件我有这个:
<GlobalNamingResources>
<Resource auth="Container" driverClassName="org.postgresql.Driver"
maxActive="100" maxIdle="5" maxWait="10000"
minEvictableIdleTimeMillis="60000" name="jdbc/myTomcatPool"
password="password" testOnBorrow="true" testWhileIdle="true"
timeBetweenEvictionRunsMillis="10000" type="javax.sql.DataSource"
url="jdbc:postgresql://localhost:5432/postgis" username="postgres"
validationQuery="SELECT 1"/>
</GlobalNamingResources>
我的中有以下内容spring-context.xml
(位于类路径上):
<jee:jndi-lookup id="geoCodeData" jndi-name="java:comp/env/jdbc/myTomcatPool" />
<bean id="geoCodeService" class="com.sample.SampleImpl">
<property name="dataSource" ref="geoCodeData"/>
</bean>
然后我把这个存入文件META-INF/context.xml
:
<Context path="/myApp" reloadable="true" cacheMaxSize="51200"
cacheObjectMaxSize="2560">
<ResourceLink global="jdbc/myTomcatPool" name="jdbc/myTomcatPool"
type="javax.sql.DataSource"/>
</Context>
我的服务器启动没有错误。
当我尝试运行以下测试时(在添加 JNDI 代码之前有效):
public class Test {
public static void main(String[] args) {
ApplicationContext ctx =
new ClassPathXmlApplicationContext("spring-context.xml");
}
}
我收到以下错误:
线程“main”中的异常org.springframework.beans.factory.BeanCreationException:创建名为“geoCodeData”的bean时出错:调用init方法失败;
嵌套异常是 javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者作为小程序参数,或者在应用程序资源文件中:java.naming.factory.initial
是我的配置错误还是我尝试运行测试的方式不正确?