gpt4 book ai didi

java - 无法找到 ServletContext 中定义的名为 XXX 的 bean 的类 XXX

转载 作者:行者123 更新时间:2023-12-02 11:02:13 25 4
gpt4 key购买 nike

我正在编写 Spring MVC 并遇到以下错误:

18:34:44,999 WARN [org.springframework.web.context.support.XmlWebApplicationContext] (MSC service thread 1-1) Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.icumed.beans.RequestInterfaceImpl] for bean with name 'scopedTarget.requestscope' defined in ServletContext resource [/WEB-INF/FetchDevice-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.icumed.beans.RequestInterfaceImpl from [Module "deployment.6.BeanScopingRequestSession.war:main" from Service Module Loader] Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.icumed.beans.RequestInterfaceImpl] for bean with name 'scopedTarget.requestscope' defined in ServletContext resource [/WEB-INF/FetchDevice-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.icumed.beans.RequestInterfaceImpl from [Module "deployment.6.BeanScopingRequestSession.war:main" from Service Module Loader] Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.icumed.beans.SessionInterfaceImpl] for bean with name 'scopedTarget.sessionscope' defined in ServletContext resource [/WEB-INF/FetchDevice-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.icumed.beans.SessionInterfaceImpl from [Module "deployment.6.BeanScopingRequestSession.war:main" from Service Module Loader]

我的目录结构:

enter image description here

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"。我认为这是错误的,因为代理!

关于java - 无法找到 ServletContext 中定义的名为 XXX 的 bean 的类 XXX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51246626/

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