Spring Jndi配置,Server.xml [英] Spring Jndi Configuration, Server.xml

查看:257
本文介绍了Spring Jndi配置,Server.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Spring设置JNDI的配置时遇到问题。我检查了其他帖子,但不能让我的问题解决。我使用Tomcat 6作为我的容器。从我的理解,我需要在服务器上设置一个资源。所以在我的 server.xml 文件中我有这样:

 < GlobalNamingResources> 
< Resource auth =ContainerdriverClassName =org.postgresql.Driver
maxActive =100maxIdle =5maxWait =10000
minEvictableIdleTimeMillis =60000name =jdbc / myTomcatPool
password =passwordtestOnBorrow =truetestWhileIdle =true
timeBetweenEvictionRunsMillis =10000type =javax.sql.DataSource
url = jdbc:postgresql:// localhost:5432 / postgisusername =postgres
validationQuery =SELECT 1/>
< / GlobalNamingResources>

我在 spring-context.xml (位于类路径上):

 < jee:jndi-lookup id =geoCodeDatajndi-name =java:comp / env / jdbc / myTomcatPool/> 

< bean id =geoCodeServiceclass =com.sample.SampleImpl>
< property name =dataSourceref =geoCodeData/>
< / bean>

然后我在文件 META-INF / context.xml

 < Context path =/ myAppoadable =true cacheMaxSize =51200
cacheObjectMaxSize =2560>
< ResourceLink global =jdbc / myTomcatPoolname =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方法失败; p>

嵌套异常是javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或作为applet参数,或在应用程序资源文件中指定类名:java.naming。 factory.initial


我的配置是否错误或是我试图运行测试的方式不正确?

解决方案

当您运行测试用例时,您将需要使用JDBC而不是JNDI查找。简单的原因是因为你通常不从应用程序服务器运行测试用例。因此,JNDI查找将失败。



我做的最后是将数据源放在单独的文件中。我有一个使用JNDI的文件: -



project-datasource.xml

 < jee:jndi-lookup id =geoCodeDatajndi-name =java:comp / env / jdbc / myTomcatPool>< / jee:jndi-lookup> 

...使用JDBC的单元测试的另一个文件: -



project-datasource-test.xml

  //使用相同的bean名称geoCodeData
< bean id =geoCodeDataclass =...>
< property name =driverClassNamevalue =.../>
< property name =urlvalue =.../>
< property name =usernamevalue =.../>
< property name =passwordvalue =.../>
< / bean>

网络应用程序将使用 project-datasource.xml 将使用 project-datasource-test.xml


I have a problem setting up my configuration for JNDI with Spring. I checked the other posts but could not get my problem solved. I am using Tomcat 6 as my container. From my understanding I need to set up a resource on the server. So in my server.xml file I have this:

<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>

I have the following in my spring-context.xml (which is on the classpath):

<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>

I then have this in file 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>

My server starts up free of errors.

When I try to run the following test (that worked before I added the JNDI code):

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx =
            new ClassPathXmlApplicationContext("spring-context.xml");
    }
}

I get the following error:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'geoCodeData': Invocation of init method failed;

nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

Is my configuration wrong or is the way I am trying to run the test incorrect?

解决方案

When you run your testcases, you will want to use JDBC instead of JNDI lookup. The simple reason is because you usually don't run your testcases from the application server. Thus, JNDI lookup will fail.

What I do on my end is to place data source in a separate file. I have one file for production that uses JNDI:-

project-datasource.xml

<jee:jndi-lookup id="geoCodeData" jndi-name="java:comp/env/jdbc/myTomcatPool"></jee:jndi-lookup>

... another another file for unit test that uses JDBC:-

project-datasource-test.xml

// use the same bean name "geoCodeData"
<bean id="geoCodeData" class="...">
    <property name="driverClassName" value="..." />
    <property name="url" value="..." />
    <property name="username" value="..." />
    <property name="password" value="..." />
</bean>

The web app will use project-datasource.xml whereas the unit test will use project-datasource-test.xml.

这篇关于Spring Jndi配置,Server.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆