各们帮忙看一下,为什么spring引用不到jdbcTemplate?

K哥 发布于 2012/07/12 09:55
阅读 4K+
收藏 0
public class DBUtils {

private String jdbcDriver;
private String jdbcUrl;
private String jdbcUser;
private String jdbcPwd;
private String dataBaseProductName;
private String appServerName;

protected JdbcTemplate jdbcTemplate;
protected LobHandler lobHandler;
protected NativeJdbcExtractor nativeJdbcExtractor;
...
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

public void setLobHandler(LobHandler lobHandler) {
this.lobHandler = lobHandler;
}

public void setNativeJdbcExtractor(NativeJdbcExtractor nativeJdbcExtractor) {
this.nativeJdbcExtractor = nativeJdbcExtractor;
}
}
---------------------------------------------------------------------------------------------
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="${pool.minPoolSize}" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="${pool.maxPoolSize}" />
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="${pool.maxIdleTime}" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="${pool.acquireIncrement}" />
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 
0 -->
<property name="maxStatements" value="${pool.maxStatements}" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="${pool.initialPoolSize}" />
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="${pool.idleConnectionTestPeriod}" />
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<property name="acquireRetryAttempts" value="${pool.acquireRetryAttempts}" />
<!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default: 
false -->
<property name="breakAfterAcquireFailure" value="${pool.breakAfterAcquireFailure}" />
<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable等方法来提升连接测试的性能。Default: 
false -->
<property name="testConnectionOnCheckout" value="${pool.testConnectionOnCheckout}" />
</bean>

<bean id="c3p0JdbcTemplate " class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0DataSource" />
</bean>

<bean id="oracleLobHandler" lazy-init="true"
class="org.springframework.jdbc.support.lob.OracleLobHandler">
<property name="nativeJdbcExtractor" ref="c3p0NativeJdbcExtractor" />
</bean>

<bean id="c3p0NativeJdbcExtractor" lazy-init="true"
class="org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor" />

<bean id="dbUtils" class="test.DBUtils ">
<property name="jdbcTemplate" ref="c3p0JdbcTemplate" />
<property name="lobHandler" ref="oracleLobHandler" />
<property name="nativeJdbcExtractor" ref="c3p0NativeJdbcExtractor" />
</bean>

</beans>
-----------------------------------------------------------------------------------------------------
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-JDBC.xml</param-value>
</context-param>

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
-----------------------------------------------------------------------------------------------------
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbHelper' defined in class path resource [applicationContext-JDBC.xml]: Cannot resolve reference to bean 'c3p0JdbcTemplate' while setting bean property 'jdbcTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'c3p0JdbcTemplate' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'c3p0JdbcTemplate' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 16 more
加载中
0
snow158
snow158
<bean id="c3p0JdbcTemplate " class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="c3p0DataSource" /> </bean>
id="c3p0JdbcTemplate " 里多了个空格

0
K哥
K哥

引用来自“Ben”的答案

<bean id="c3p0JdbcTemplate " class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="c3p0DataSource" /> </bean>
id="c3p0JdbcTemplate " 里多了个空格

,太感谢了。。。

但是现在又有个问题了:DBUtils 类里有这样一个方法:

public List query(String sql) throws DataAccessException {

return jdbcTemplate.queryForList(sql);

}

为什么在引用的时候,jdbcTemplate报空指针错误呢?大哥再帮忙看一下咯!

0
snow158
snow158

引用来自“34176470”的答案

引用来自“Ben”的答案

<bean id="c3p0JdbcTemplate " class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="c3p0DataSource" /> </bean>
id="c3p0JdbcTemplate " 里多了个空格

,太感谢了。。。

但是现在又有个问题了:DBUtils 类里有这样一个方法:

public List query(String sql) throws DataAccessException {

return jdbcTemplate.queryForList(sql);

}

为什么在引用的时候,jdbcTemplate报空指针错误呢?大哥再帮忙看一下咯!

相关代码和异常贴出来看看
0
w
wangqimin
报空指针 肯定是没有初始化  jdbcTemplate  你是怎么获取的呢?元注释? 
情天大圣
情天大圣
依赖注入
返回顶部
顶部