java org.springframework.beans.factory.NoSuchBeanDefinitionException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10680975/
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-10-31 02:04:50  来源:igfitidea点击:

org.springframework.beans.factory.NoSuchBeanDefinitionException

javaspring

提问by Seho Lee

I got this error when I ran my very first spring example code. It looks like spring does not

当我运行我的第一个 spring 示例代码时,我遇到了这个错误。春天好像没有

reconize a bean somehow but I dont know why. I made a bean definition file and added one bean there.

以某种方式 reconize 一个 bean,但我不知道为什么。我制作了一个 bean 定义文件并在那里添加了一个 bean。

Then I located that file under WEBINF folder. Supposely this should work without any issue but looks

然后我在 WEBINF 文件夹下找到该文件。假设这应该没有任何问题,但看起来

like I got some issues here. below is my bean definition file and jsp. can anybody tell me

就像我在这里遇到了一些问题。下面是我的 bean 定义文件和 jsp。谁能告诉我

what am I doing wrong?

我究竟做错了什么?

applicationContext.xml

应用上下文.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="HelloSpring" class="com.spring.toby.HelloSpring"></bean>

</beans>

HelloSpring.java

你好Spring.java

package com.spring.toby;

public class HelloSpring {
public String sayHello(String name){
    return "Hello " + name;
}
}

HelloSpring.jsp

HelloSpring.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="org.springframework.context.ApplicationContext" %>    
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>    
<%@ page import="com.spring.toby.HelloSpring" %>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(
    request.getSession().getServletContext());
    out.println("check");
    HelloSpring helloSpring = context.getBean(HelloSpring.class);

    //out.println(helloSpring.sayHello("seho"));
%>
</body>
</html>

web.xml

网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

and here is my error log

这是我的错误日志

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/hellospring.jsp at line 17

org.apache.jasper.JasperException:在第 17 行处理 JSP 页面 /WEB-INF/views/hellospring.jsp 时发生异常

14: ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext( 15: request.getSession().getServletContext()); 16: out.println("check"); 17: HelloSpring helloSpring = context.getBean(HelloSpring.class); 18:
19: //out.println(helloSpring.sayHello("seho")); 20: %>

14: ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext( 15: request.getSession().getServletContext()); 16: out.println("检查"); 17: HelloSpring helloSpring = context.getBean(HelloSpring.class); 18:
19: //out.println(helloSpring.sayHello("seho")); 20:%>

Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1157)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:927)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.spring.toby.HelloSpring] is defined: expected single bean but found 0: 
    org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271)
    org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1101)
    org.apache.jsp.WEB_002dINF.views.hellospring_jsp._jspService(hellospring_jsp.java:81)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1157)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:927)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

回答by Jigar Joshi

You need to add applicationContext.xmlin contextConfigLocation

您需要applicationContext.xml在 contextConfigLocation添加

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml,
                 /WEB-INF/spring/applicationContext.xml
     </param-value>
</context-param>

Alternatively you could also import bean defination from another file using <import:resource>tag in xml file

或者,您也可以使用<import:resource>xml 文件中的标签从另一个文件导入 bean 定义



Suggestion

建议

It is good practice to put logic on controllers (Servlet, Controller Classes..) not on JSP

将逻辑放在控制器(Servlet、控制器类……)而不是 JSP 上是一种很好的做法