繁体   English   中英

尝试注入具有@Required批注的bean,但获取nullpointer

[英]Trying to inject a bean with @Required annotation but getting nullpointer

这是我的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" id="WebApp_ID" version="3.0">
  <display-name>loungeHotel</display-name>
  <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
  </servlet>
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/loungeHotel.xml
        </param-value>
    </context-param>
  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

这是我的loungeHotel.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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    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">


<bean id = "updateProfileDao"  class="com.lounge.dao.impl.UpdateProfileDaoImpl"></bean>

<bean id = "updateProfileFacade"  class="com.lounge.facades.impl.UserProfileFacadeImpl">
    <property name="updateProfileDao" ref="updateProfileDao"></property>
</bean>

</beans>    

这是我要使用我的bean( updateProfileDao )的Java文件( UserProfileFacadeImpl.java

package com.lounge.facades.impl;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Required;

import com.lounge.dao.impl.UpdateProfileDaoImpl;
import com.lounge.dataModels.StudentData;
import com.lounge.facade.UserProfileFacade;

public class UserProfileFacadeImpl implements UserProfileFacade {

    @Resource(name="updateProfileDao")
    private UpdateProfileDaoImpl updateProfileDao;


    public UpdateProfileDaoImpl getUpdateProfileDao() {
        return updateProfileDao;
    }

    @Required
    public void setUpdateProfileDao(UpdateProfileDaoImpl updateProfileDao) {
        this.updateProfileDao = updateProfileDao;
    }
    @Override
    public StudentData updateProfile(StudentData studentData,String name){
        return getUpdateProfileDao().updateColumn(studentData, name);
    }
}

这是stacktrace ...

>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

>java.lang.**NullPointerException**
    com.lounge.facades.impl.UserProfileFacadeImpl.updateProfile(UserProfileFacadeImpl.java:30)
    com.lounge.controllers.AccountPageController.updateProfile(AccountPageController.java:63)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)

实际上, @Resource并不重要,因为您在xml中显式设置了属性。

我认为根本原因在于控制器,最有可能直接实例化该对象,而不是通过应用程序容器将其注入。

由于您使用的是SpringMVC,因此建议您使用@Autowired将Dao作为服务加载,而不要使用@Resource

根据您的评论,您正在通过new运算符访问UserProfileFacadeImpl。 这违反了依赖项注入的目的以及遇到NPE的原因。 应该通过在Spring容器中获取UserProfileFacadeImpl来对其进行访问。

将类型为UserProfileFacadeImpl的bean添加到您的spring配置文件(loungeHotel.xml)。

注入此bean而不是调用new。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM