簡體   English   中英

找不到在ServletContext中定義的名稱為XXX的bean的類XXX

[英]Cannot find Class XXX for bean with the name XXX defined in ServletContext

我正在編寫Spring MVC並遇到以下錯誤:

18:34:44,999警告[org.springframework.web.context.support.XmlWebApplicationContext](MSC服務線程1-1)上下文初始化期間遇到異常-取消刷新嘗試:org.springframework.beans.factory.BeanCreationException:創建bean時出錯名稱為“ org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0”:Bean初始化失敗; 嵌套的異常是org.springframework.beans.factory.CannotLoadBeanClassException:找不到在ServletContext資源[/WEB-INF/FetchDevice-servlet.xml]中定義的名稱為'scopedTarget.requestscope'的bean的類[com.icumed.beans.RequestInterfaceImpl]。 ; 嵌套的異常是java.lang.ClassNotFoundException:com.icumed.beans.RequestInterfaceImpl來自Service Module Loader的[Module“ deployment.6.BeanScopingRequestSession.war:main”]相關原因:org.springframework.beans.factory.CannotLoadBeanClassException:找不到在ServletContext資源[/WEB-INF/FetchDevice-servlet.xml]中定義的名稱為'scopedTarget.requestscope'的bean的類[com.icumed.beans.RequestInterfaceImpl]; 嵌套的異常是java.lang.ClassNotFoundException:com.icumed.beans.RequestInterfaceImpl來自Service Module Loader的[Module“ deployment.6.BeanScopingRequestSession.war:main”]相關原因:org.springframework.beans.factory.CannotLoadBeanClassException:找不到在ServletContext資源[/WEB-INF/FetchDevice-servlet.xml]中定義的名稱為'scopedTarget.sessionscope'的bean的類[com.icumed.beans.SessionInterfaceImpl]; 嵌套的異常是java.lang.ClassNotFoundException:com.icumed.beans.SessionInterfaceImpl,來自[Service Module Loader中的模塊“ deployment.6.BeanScopingRequestSession.war:main”]

我的目錄結構:

在此處輸入圖片說明

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"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
  <display-name>ICUMED-Req-Session-scope</display-name>
  <servlet>
    <servlet-name>FetchDevice</servlet-name>
    <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>FetchDevice</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>
</web-app>

FetchDevice-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:aop="http://www.springframework.org/schema/aop"
   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
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 <mvc:annotation-driven />

<bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
</bean>

<context:component-scan base-package="com.icumed.beans" />
<context:annotation-config />

<bean id="requestscope" class="com.icumed.beans.RequestInterfaceImpl" scope="request">
  <constructor-arg value="Device1"/>
   <aop:scoped-proxy proxy-target-class="false" />
</bean>

<bean id="sessionscope" class="com.icumed.beans.SessionInterfaceImpl" scope="session">
  <constructor-arg value="Device2"/>
   <aop:scoped-proxy proxy-target-class="false" />
</bean>
</beans>

TestController.java

package com.icumed.beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

    @Autowired(required=true)
    private RequestInterfaceImpl requestInterfaceImpl;

    @Autowired(required=true)
    private SessionInterfaceImpl sessionInterfaceImpl;

    @RequestMapping(value = "/")
    public String fetchDevice(Model model) {
        model.addAttribute("requestscopedData", requestInterfaceImpl.getDeviceName());
        model.addAttribute("sessionscopedData", sessionInterfaceImpl.getDeviceName());

        /* return to view "hello.jsp" */
        return "device";
    }

}

首先:您可以更改過濾器。 有時它需要那些。

<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filterclass>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>      //filter path
</filter-mapping>

第二:您知道RequestInterfaceImpl@Autowired 它需要代理; 您設置proxy-target-class="false" ,因此它將使用JDK代理而不是CGLib。

您可以這樣做:

@Autowired(required=true)
private RequestInterface requestInterface;   // use its interface, not impl

或者,您可以設置proxy-target-class="true" 我認為是因為代理錯誤!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM