gpt4 book ai didi

java - bean实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException : Could not instantiate bean class

转载 作者:行者123 更新时间:2023-11-30 08:38:36 26 4
gpt4 key购买 nike

我正在做 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

Controller 类

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)

package services.ap.in.xyz.dao;

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

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;
}

登录.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

<?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>

网络.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>

最佳答案

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

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

关于java - bean实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException : Could not instantiate bean class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36475260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com