我有一個問題,春節建立我的配置JNDI。我查了其他帖子,但無法解決我的問題。我使用Tomcat 6作爲我的容器。根據我的理解,我需要在服務器上設置資源。所以在我的server.xml
文件我有這樣的:春JNDI配置,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
(這是在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>
然後我有這個文件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");
}
}
我收到以下錯誤:
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
是我的配置錯誤,或在我試圖運行測試不正確?
這很有道理。我認爲這可能與測試案例有關。我會試試這個。我將把這標記爲答案。再次感謝。最後一個問題,我會在我的web.xml中添加project-datasource.xml到contextConfigLocation的列表中,並在我的測試類中使用project-datasource-test.xml,就像我的例子一樣? – blong824 2011-03-04 19:22:46
多聰明的解決方案!順便說一句,你可以使用:''等方式引用你的app config xml中新創建的數據源文件。乾杯! –
acdcjunior
2013-12-04 20:46:00