Java bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 bean 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36475260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:54:43  来源:igfitidea点击:

Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class

javaxmlspringjspspring-mvc

提问by Pradeep

I am doing spring login application. When I run the application on WAS server I am getting some exceptions. Errors

我正在做 spring 登录应用程序。当我在 WAS 服务器上运行应用程序时,出现了一些异常。错误

org.springframework.web.servlet.FrameworkServlet initServletBean Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xyzAccessDao' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [services.ap.in.xyz.dao.impl.XYZAccessManagementDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException

org.springframework.web.servlet.FrameworkServlet initServletBean 上下文初始化失败 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/spring-servlet.xml] 中定义名称为“xyzAccessDao”的 bean 创建时出错:实例化豆子失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 bean 类 [services.ap.in.xyz.dao.impl.XYZAccessManagementDaoImpl]:构造函数抛出异常;嵌套异常是 java.lang.NullPointerException

Controller Class

控制器类

package services.ap.in.xyz.action;

@Controller 
public class XYZLoginAction {


  XYZAccessManagementDaoImpl xyzAccessDao;

  @RequestMapping("/login")  
  public ModelAndView processAction(HttpServletRequest request,HttpServletResponse res) {

        String loggedInUserId="";
        String pwd="";
        String NotesId = null;
        boolean authFlag=false;


      if (request.getParameter("loginSubmit") != null ){
      System.out.println(" processAction :: loginSubmit action"); 

      try{
          loggedInUserId = (String) request.getParameter("userName");
          pwd     = (String) request.getParameter("password");

          if(loggedInUserId!=null && !loggedInUserId.equalsIgnoreCase("")&&pwd!=null && !pwd.equalsIgnoreCase(""))
            {

                NotesId = xyzAccessDao.getNotesId(loggedInUserId);
                 String message = "HELLO no role for "+NotesId;  
                  return new ModelAndView("welcome", "message", message);       
            }
                    else{
                     return new ModelAndView("errorpage", "message","Sorry, username or password error");  
                    }

                }

            }
      }catch (Exception e) {
          System.out.println(" processAction :: Exception ::"+e.getMessage()+e.getStackTrace());
      }
               return new ModelAndView("errorpage", "message","Sorry, username or password error");  
}

DAO Interface

DAO接口

package services.ap.in.xyz.dao;

public interface XYZAccessManagementDao {
    public String getNotesId(String userId) throws Exception;   
}

DAO Impl

DAO 实现

package services.ap.in.xyz.dao.impl;

import services.ap.in.xyz.action.XYZLoginAction;
import services.ap.in.xyz.dao.XYZAccessManagementDao;

public class XYZAccessManagementDaoImpl implements XYZAccessManagementDao{

    @Override
    public String getNotesId(String loggedInUserId) throws Exception {
        System.out.println(" getNotesId() :: Entered." + loggedInUserId);
        String notesId = "";
        try {
           // get notesID
            }
        } catch (Exception e) {

        }
        return notesId;
 }

login.jsp

登录.jsp

 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
            <title>Login</title>
        </head>
        <body>
             <form action="login.html" method="post">  
                Name:<input type="text" name="userName"/><br/>  
                Password:<input type="password" name="password"/><br/>  
                         <input type="submit" value="login" name="loginSubmit"/>  
            </form>
        </body>
    </html>

spring-servlet.xml

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.0.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    <context:component-scan  base-package="services.ap.in.gbsgdbcms.action" />  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/jsp/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  
     <bean id="bcmAccessDao" class="services.ap.in.xyz.dao.impl.XYZAccessManagementDaoImpl" />  
</beans>  

web.xml

网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
    <display-name>LoginExample</display-name>
    <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>*.html</url-pattern>  
</servlet-mapping>  
</web-app>

回答by Kannan Thangadurai

Change the base package <context:component-scan base-package="services.ap.in.gbsgdbcms.action" />like <context:component-scan base-package="services.ap.in" />

更改基本包,<context:component-scan base-package="services.ap.in.gbsgdbcms.action" /><context:component-scan base-package="services.ap.in" />

The above declaration in the spring application configuration file would scan the classes inside the specified package and create the beans instance.

spring应用程序配置文件中的上述声明将扫描指定包内的类并创建bean实例。

回答by KayV

Use @Autowired with "XYZAccessManagementDaoImpl xyzAccessDao" in you controller as follows:

在控制器中使用带有“XYZAccessManagementDaoImpl xyzAccessDao”的@Autowired,如下所示:

@Autowired
XYZAccessManagementDaoImpl xyzAccessDao;

And use @Repository with DaoImpl class definition as follows:

并将 @Repository 与 DaoImpl 类定义一起使用,如下所示:

@Repository
public class XYZAccessManagementDaoImpl implements XYZAccessManagementDao{