通过添加到列表为servlet调度程序分配异常

时间:2015-08-03 11:39:05

标签: java spring spring-mvc nullpointerexception dao

我在将数据添加到列表中时遇到问题。 控制器类:

package com.test.sample.controller;

@Controller
public class HomeController {

@Autowired
UserService userService;

@RequestMapping(value="/add/{user}", method=RequestMethod.GET)
public ModelAndView addUser(@PathVariable String user) {
    userService.addUser(user);
    ModelAndView model = new ModelAndView();
    model.setViewName("userPage");
    model.addObject("user", "User '" + user + "' was saved.");
    return model;
}

}

服务lloks如:

package com.test.sample.service.impl;

@Service("userService")
public class UserServiceImpl implements UserService{

@Autowired
UserDao userDao;

@Autowired
public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}

@Override
public void addUser(String name) {
    userDao.addUser(name);
}
}

Dao的实施主要是:

package com.test.sample.dao.impl;

@Repository("userDao")
public class UserDaoImpl implements UserDao{

@Autowired
UserContainer container;

List<UserBean> list;

public UserDaoImpl() {
    list = container.getUserList(); //exception in this line
}

@Override
public void addUser(String name) {
    UserBean userBean = new UserBean();
    userBean.setName(name);
    list.add(userBean);
}
}

容器类:

package com.test.sample.container;

@Repository("userContainer")
public class UserContainer {

    private List<UserBean> userList = new LinkedList<UserBean>();

    public List<UserBean> getUserList() {
        return userList;
    }

    public void setUserList(List<UserBean> userList) {
        this.userList = userList;
    }
    }

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

  <display-name>Archetype Created Web Application</display-name>

  <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

调度员servlet:

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

    <context:component-scan base-package="com.test" />

    <bean id="userService" class="com.test.sample.service.impl.UserServiceImpl" />
    <bean id="userDao" class="com.test.sample.dao.impl.UserDaoImpl" />

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

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />
</beans>

现在是exseption:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: com.test.sample.service.UserService
    com.test.sample.controller.HomeController.userService; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.test.sample.dao.UserDao
    com.test.sample.service.impl.UserServiceImpl.userDao; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'userDao' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is
    org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [com.test.sample.dao.impl.UserDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException

Allocate exception for servlet mvc-dispatcher
java.lang.NullPointerException
    at com.test.sample.dao.impl.UserDaoImpl.<init>(UserDaoImpl.java:21)

1 个答案:

答案 0 :(得分:1)

因为这个错误就是这个:

public UserDaoImpl() {
    list = container.getUserList(); //exception in this line
}

在界面UserDao中,请添加方法List<UserBean> getUsers

并实现这一点:

@Repository
public class UserDaoImpl implements UserDao{

@Autowired
UserContainer container;

@Override
public List<UserBean> getUsers(){
    return container.getUserList();
}

@Override
public void addUser(String name) {
    UserBean userBean = new UserBean();
    userBean.setName(name);
    getUsers().add(userBean);
}
}

我真的不明白UserContainer目的。我认为这非常多余,因为您可以在UserDaoImpl中执行所有操作。