org.springframework.beans.factory.BeanCreationException:使用名称' cpDataSource'创建bean时出错:init方法的调用失败;嵌套 异常是javax.naming.NameNotFoundException: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 :(得分: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" />