MVC Spring Implementation - 错误:java.lang.NullPointerException

时间:2014-02-23 06:21:33

标签: java spring-mvc

我有一个MVC Spring控制器,在Imp文件中,我有:

import com.ish.system.dao.UserDAO;

public class CustomImpl implements CustomService {

UserDAO   userDAO = null;

public UserDAO getUserDAO() {
  return userDAO;
}

public void setUserDAO(UserDAO userDAO) {
  this.userDAO = userDAO;
}

public String ServiceType(userId) {

User user = userDAO.findById(userId);

...

以下是控制台中的错误:

SEVERE: Servlet.service() for servlet dispatcherServlet threw exception
java.lang.NullPointerException
    at com.ish.smdb.service.impl.CustomImpl.ServiceType(CustomImpl.java:142)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)

这是控制器:

@RequestMapping(value="/ServiceType", method=RequestMethod.GET)
    public @ResponseBody String ServiceType(

            Locale locale,
            Model model) {
        String result = custom.ServiceType();

    return result;
}

这是豆子:

<bean id="Custom" parent="baseTransactionProxy"> 
  <property name="target"> 
    <bean id="CustomImpl" class="com.is.sm.service.impl.CustomImpl"> 
      <property name="userDAO" ref="UserDAO" /> 
    </bean> 
  </property> 
</bean>

肯定userDAO为空。但我没有找到共鸣。我错了什么?

1 个答案:

答案 0 :(得分:0)

CustomImpl bean依赖于UserDAO,因此必须将UserDAO bean注入CustomImpl bean, 请发布您已定义bean的xml配置文件。

更新

这是一个解决方案,您可以使用spring注释进行注入:

在您的UserDAOImpl calss(UserDAO接口implementsatoin)中使用:

@Repository(value = "userDAO")
public class UserDAOImpl implements UserDAO {....}

然后在你的CustomImpl类中:

@Autowired
UserDAO userDAO ;