【发布时间】:2014-05-29 07:37:36
【问题描述】:
我在使用 Spring 3.1 查找 DataSource 和使用 Tomcat 查找 JNDI 时遇到问题。即使我已经为其提供了 jar,它也无法找到 HSQL db 的驱动程序类。
我尝试将 jar 放入我的 Maven pom.xml 中,并将 jar 复制到 ${CATALINA_HOME}/lib 目录。但似乎没有任何效果。我的堆栈跟踪如下所示。
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'EProfileConfigureSimulator':
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire method: public void com.myproject.simulator.ConfigureSimulator.setDataSource(javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create JDBC driver of class '' for connect URL 'jdbc:hsqldb:hsql://localhost:9001/SIMULATORDB'
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
. . . .
我在 $CATALINA_HOME/conf/context.xml 中的 Tomcat 配置
<Resource name="jdbc/SimulatorDB" auth="Container"
type="javax.sql.DataSource"
driveClassName="org.hsqldb.jdbc.JDBCDriver"
url="jdbc:hsqldb:hsql://localhost:9001/SIMULATORDB"
username="SA"
password=""
initialSize="25"
maxActive="100"
maxIdle="30"
maxWait="10000" />
web.xml
<resource-ref>
<description>HSQL Database</description>
<res-ref-name>jdbc/SimulatorDB</res-ref-name> <!-- Must match the Server Resource name -->
<res-type>javax.sql.DataSource</res-type> <!-- Must match the Server type -->
<res-auth>Container</res-auth> <!-- Must match the server auth attribute. -->
</resource-ref>
applicationContext.xml
<!-- Enable annotations -->
<context:annotation-config />
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/SimulatorDB" expected-type="javax.sql.DataSource" />
<!--
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/SimulatorDB"/>
<property name="lookupOnStartup" value="true"/>
<property name="resourceRef" value="true" />
<property name="expectedType" value="javax.sql.DataSource" />
<property name="proxyInterface" value="javax.sql.DataSource"/>
</bean> -->
我的源代码
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
class JdbcHelper {
private DataSource dataSource;
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public DataSource getDataSource() {
return this.dataSource;
}
}
【问题讨论】:
标签: tomcat spring-mvc jndi