2017-10-04 76 views
0

org.springframework.beans.factory.BeanCreationException:创建名为'cpDataSource'的bean时出错:init方法的调用失败;嵌套0​​例外是javax.naming.NameNotFoundException:JDBC/FCjavax.naming.NamingNotFoundException:jdbc/fc

我使用wildfly 10和这里的web.xml。

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <context-param id="ParamValue_1395228155626"> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>WEB-INF/applicationContext.xml</param-value> 
    </context-param> 

<resource-ref> 
    <description>Oracle example</description> 
    <res-ref-name>jdbc/fc</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 
<servlet id="Init"> 
    <servlet-name>Init Servlet</servlet-name> 
    <servlet-class>com.cedar.cp.utc.common.InitServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet> 
    <servlet-name>reviewbudget</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>9</load-on-startup> 
</servlet> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 
</web-app> 

这context.xml的

<?xml version='1.0' encoding='utf-8'?> 
<Context crossContext="true" reloadable="true"> 
    <WatchedResource>WEB-INF/web.xml</WatchedResource> 

    <Resource name="java:jboss/jdbc/fc" auth="Container" 
     type="javax.sql.DataSource" 
     driverClassName="oracle.jdbc.driver.OracleDriver" 
     url="jdbc:oracle:thin:@127.0.0.1:1521:orcl" 
     username="C##user" password="windows7password" maxActive="20" maxIdle="10" 
     maxWait="-1"/> 
</Context> 

这里的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

<jee:jndi-lookup id="cpDataSource" jndi-name="java:jboss/jdbc/fc" 
    expected-type="javax.sql.DataSource" /> 
<jee:jndi-lookup id="oaDataSource" jndi-name="java:jboss/jdbc/fc" 
    expected-type="javax.sql.DataSource" /> 

<tx:annotation-driven transaction-manager="transactionManager" /> 

<bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="cpDataSource" /> 
</bean> 
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > 
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> 
    <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/> 
    <property name="username" value="C##user1"/> 
    <property name="password" value="windows7password"/> 
</bean> 


</beans> 

我怎样才能解决这个问题? 谢谢。

+0

我不知道了很多关于春天,但'context.xml'一个Tomcat的事情? WildFly肯定不会寻找'context.xml'。 –

+0

感谢您的回复,但wildfly现在正确查找context.xml。 –

回答

0

在你web.xml

<resource-ref> 
    <description>Oracle example</description> 
    <res-ref-name>jdbc/fc</res-ref-name><!-- This should be available as java:comp/env/jdbc/fc in the JNDI context --> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 

所以,你可以尝试把这个在applicationContext.xml

<jee:jndi-lookup id="cpDataSource" jndi-name="java:comp/env/jdbc/fc" 
expected-type="javax.sql.DataSource" /> 
<jee:jndi-lookup id="oaDataSource" jndi-name="java:comp/env/jdbc/fc" 
expected-type="javax.sql.DataSource" />