spring注入jdbctemplate空指针

aaaa8215 2012-09-17 01:20:35
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;

@Service("dbAccess")
public class DBAccess
{
public int pidx = 1;
public Connection conn = null;
private boolean __count_records__ = false;
private PreparedStatement __prod__ = null;

@Autowired
private JdbcTemplate jdbcTemplate;

public synchronized void openConnection() throws Exception
{
System.out.println("jdbcTemplate :" + jdbcTemplate);
conn = jdbcTemplate.getDataSource().getConnection();
pidx = 1;
}

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="sms.framework" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/webdb"/>
<property name="username" value="root"/>
<property name="password" value="900120"/>
<property name="initialSize" value="10"/>
<property name="maxActive" value="50"/>
<property name="maxIdle" value="10"/>
<property name="maxWait" value="1000"/>
<property name="poolPreparedStatements" value="true"/>
</bean>


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

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="dbAccess" class="sms.framework.data.DBAccess">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>


打印jdbctemplate一直是空的,是什么地方配置错了吗?
...全文
797 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ztwsl888 2014-10-27
  • 打赏
  • 举报
回复
我也遇到这个问题 郁闷啊
yaerfeng 2014-05-04
  • 打赏
  • 举报
回复
可以参考代码:Spring JdbcTemplate框架搭建及其增删改查使用指南,下载地址:http://www.zuidaima.com/share/1724429678644224.htm
zhanglujie2008 2012-09-21
  • 打赏
  • 举报
回复
getter setter 方法哪去了?
chenbo290175 2012-09-21
  • 打赏
  • 举报
回复
我遇到一模一样的问题,就是你调用的时候,你不要去new 一个已经注入的对象,那样就会出现空指针。所有的都要才用自动注入的形式 @
jya126 2012-09-17
  • 打赏
  • 举报
回复
你通过set或get方式注入jdbcTemplate试试
aaaa8215 2012-09-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

没有读取到配置文件中内容
[/Quote]我debug了读取到了配置文件 并且xml文件初始化成功
m540920181 2012-09-17
  • 打赏
  • 举报
回复
没有读取到配置文件中内容
brightyq 2012-09-17
  • 打赏
  • 举报
回复
看看例子

http://www.cnblogs.com/Fskjb/archive/2009/11/18/1605622.html
aaaa8215 2012-09-17
  • 打赏
  • 举报
回复
没人? 求指教 ,散分全给

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧