最近刚开始学spring,然后用它来整合jdbcTemplate,遇到了一个很奇怪的问题,
用 junit 在本地一个个函数的测试的时候,没报错;然后把项目部署到Tomcat上后,发现全部bean都报空指针的错误。
以下是部分代码首先是applicationContext的部分配置<!-- 1.将连接池 -->
<bean name="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="jdbc:mysql://119.23.229.19:3306/fade?useUnicode=true&amp;characterEncoding=utf-8" ></property>
<property name="driverClass" value="com.mysql.jdbc.Driver" ></property>
<property name="user" value="student" ></property>
<property name="password" value="137137lu" ></property>
<property name="maxPoolSize" value="100" ></property>
</bean>
<!-- 2.将JDBCTemplate放入spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource1" ></property>
</bean>
<!-- 3.Dao-->
<bean name="userDao" class="com.hl.dao.UserDaoImpl">
   <property name="dataSource" ref="dataSource1"></property>
  <!--  <property name="jt" ref="jdbcTemplate" ></property> -->
</bean><!-- 4.Service -->
<bean name="userService" class="com.hl.service.UserServiceImpl">
   <property name="userDao" ref="userDao"></property>
</bean><!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
<bean name="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource1"></property>
</bean><!-- 开启使用注解管理aop事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
UserDao继承了jdbcDaoSurpprt接口public class UserDaoImpl extends JdbcDaoSupport implements UserDao{


//查找手机号是否已被注册
@Override
public Object isFindUserByTel(String telephone) {
// TODO Auto-generated method stub
String sql = "select*from user where telephone = ? ";
try {
return this.getJdbcTemplate().queryForObject(sql, new UserRowMapper(),telephone);
} catch (Exception e) {
return null;
}

}
UserService的代码如下,getter和setter也写好了。public class UserServiceImpl implements UserService {
private UserDao userDao;
    public UserDao getUserDao() {
return userDao;
}
@Override
public String registerQueryTel(User user) {
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// userDao = (UserDao) applicationContext.getBean("userDao");
JSONObject jsonObject = new JSONObject();
try {
if(userDao.isFindUserByTel(user.getTelephone()) == null){
jsonObject.put("ans", 0);      //没有注册   
}else{
jsonObject.put("ans", 1);      //已经注册
}
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString();
}
web.xml配置的监听器如下:  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
当我在本地编写测试代码调用的时候,是有返回结果的,也没报错,感觉applicationContext配置没问题 @Test
public void test8() throws Exception {
User user = new User();
user.setTelephone("13763359943");
System.out.println(service.registerQueryTel(user));
}
可是用浏览器打开访问时,就报了空指针错误,userDao为空。
我觉得原因是监听器没有生成对象工厂applicationContext,但是又不知道哪里错了。

解决方案 »

  1.   

    UserServiceImpl  报错 93行代码没上全吧
    从现在的UserServiceImpl 来看,UserDao也没有set 方法吧,空也是很正常啊
      

  2.   

    我也是初学,应该是注入失败,所以是空的,你是不是用的类似 jUnit做的测试,这样做spring好像没有生效,必须用web方式,让web.xml的配置生效才能注入成功。或者applicationContext.getBean注入,希望对你有帮助
      

  3.   

    如果是本地测试的话要初始化ApplicationContext,
      

  4.   

    说错了不是初始化,是要将bean注入