org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException我知道可能是注入数据源没成功,大家帮我看看呢,纠结了很久了。解决不了。WebXml配置<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--指定相应的spring配置文件位置 默认路径/WEB-INF/applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list></web-app>spring-servlet.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"
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:component-scan base-package="com.bxmis.controller" />
<bean id="viewResolver"  class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix"    value="/WEB-INF/views/" />
<property name="suffix"    value=".jsp" />
</bean>

</beans>applicationContext.xml[code=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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!--  配置数据源   -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
<!-- 连接池启动时的初始值   -->
<property name="initialSize" value="1" />
<!-- 连接池的最大值  -->
<property name="maxActive" value="500" />
<!-- 最大空闲值,当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接释放,一直减少到msxIdle为止  -->
<property name="maxIdle" value="2" />
<!-- 最小空闲值,当空闲的连接数小于阀值时,连接池就会预申请一些连接,以免洪峰到来时来不及申请  -->
<property name="minIdle" value="1" />
</bean> <bean id="userInfoService" class="com.bxmis.service.impl.UserInfoServiceImpl">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean></beans>
最后这是UserInfoServiceImpl 写的一个简单实现package com.bxmis.service.impl;import java.util.List;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class UserInfoServiceImpl extends JdbcDaoSupport {
@SuppressWarnings( { "unchecked", "rawtypes" })
public List listUser() {
// TODO Auto-generated method stub String sql = "select * from user";
List list = getJdbcTemplate().query(sql, new UserRowMapper()); return list;
}}
MVC Controller调用 UsrInfoSerceImpl
@RequestMapping("/test")
public ModelAndView test(){




UserInfoServiceImpl userInfo = new UserInfoServiceImpl();
userInfo.listUser();
return new ModelAndView("test","message","This is a test page!!!");

 
}[/code]一直就是报错误,自己解决不了。谢谢各位大牛。

解决方案 »

  1.   


     UserInfoServiceImpl userInfo = new UserInfoServiceImpl();改成
    @Autowired
    private  UserInfoServiceImpl;
    然后调用,有报这个错误org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BXTestController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bxmis.service.impl.UserInfoServiceImpl com.bxmis.controller.BXTestController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.bxmis.service.impl.UserInfoServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      

  2.   

    LZ把DAO继承改为SimpleJdbcDaoSupport,然后调用getSimpleJdbcTemplate().update()试试