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

时间:2016-04-07 11:46:23

标签: java xml spring jsp spring-mvc

我正在做春季登录申请。当我在WAS服务器上运行应用程序时,我遇到了一些例外。错误

  

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

控制器类

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接口

package services.ap.in.xyz.dao;

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

DAO Impl

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

 <%@ 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>

弹簧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 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>

2 个答案:

答案 0 :(得分:1)

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

等基础包<context:component-scan base-package="services.ap.in" />

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

答案 1 :(得分:1)

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

@Autowired
XYZAccessManagementDaoImpl xyzAccessDao;

使用@Repository和DaoImpl类定义如下:

@Repository
public class XYZAccessManagementDaoImpl implements XYZAccessManagementDao{