在ServletContext资源[/WEB-INF/ApplationConext.xml]中找不到名为‘foodDAO’的Bean的类[food.bill.dao.FoodDaoImpl];

cuxqih21  于 2022-10-23  发布在  Spring
关注(0)|答案(4)|浏览(100)

我正在学习Spring,却陷入了一个错误。Spring无法在Bean中找到定义的类:

31-Mar-2018 20:12:44.049 SEVERE [RMI TCP Connection(3)-127.0.0.1] 
org.springframework.web.context.ContextLoader.initWebApplicationContext 
Context initialization failed
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [food.bill.dao.FoodDaoImpl] for bean with name 'foodDAO' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: food.bill.dao.FoodDaoImpl
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1385)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:641)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:609)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1484)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1007)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:741)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4753)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5215)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1736)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: food.bill.dao.FoodDaoImpl
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:401)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1432)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1377)
... 55 more

下面是我的applationConext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <bean id="foodDAO" class="food.bill.dao.FoodDaoImpl"/>

    <bean id="foodService" class="food.bill.service.FoodServiceImpl">
        <property name="foodDao" ref="foodDAO"/>
    </bean>

</beans>

FoodDaoImpl:

package food.bill.dao;

import food.bill.model.FoodItem;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

public class FoodDaoImpl implements FoodDao {

    private static final Logger log = LogManager.getLogger(FoodDaoImpl.class);
    @PersistenceContext
    EntityManager em;

    public List<FoodItem> getMenu() {
        List<FoodItem> foodMenu = null;
        try {
            Query query = em.createNamedQuery("FoodItem.getMenu");
                 foodMenu = (List<FoodItem>) query.getResultList();
            return foodMenu;
        }catch (Exception e){
            log.info("Error in getting Menu");
        }
        return foodMenu;
    }

    public void createOrder() {

    }
}

FoodServiceImpl:

package food.bill.service;

import food.bill.dao.FoodDao;
import food.bill.model.FoodItem;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {

   private FoodDao foodDao;

    public void setFoodDao(FoodDao foodDao) {
        this.foodDao = foodDao;
    }

    public List<FoodItem> getMenu() {
        return this.foodDao.getMenu();
    }
}

我已经检查了输出文件夹,所有文件都已更新。我试着找了几个小时,但没有找到任何结果。任何帮助都将不胜感激。先谢谢你。

xriantvc

xriantvc1#

Org.springframework.beans.factory.CannotLoadBeanClassException:找不到ServletContext资源[/WEB-INF/ApplationConext.xml]中定义的名为‘FoodDAO’的Bean的类[Food.bill.dao.FoodDaoImpl];嵌套异常为java.lang.ClassNotFoundException:Food.bill.dao.FoodDaoImpl
简单地说,这意味着Spring试图在您定义的类路径中为类food.bill.dao.FoodDaoImpl“找到”.class文件,但没有找到。
因此,请首先检查food.bill.dao.FoodDaoImpl文件是否真的编译为.class文件,以及它在输出文件目录中的位置。

ar7v8xwq

ar7v8xwq2#

有几件事需要仔细检查
1.如果您使用的是Maven或任何其他构建工具,请尝试使用“Maven lean”,并检查它是否删除了foodDao.class文件。如果是,则在清理后执行Maven构建,并查看它是否修复了该问题。
1.如果步骤1不能解决问题,请尝试将foodDAO的Bean定义放在context.xml中的FoodService的Bean定义之后(这只是一个想法)
如果问题仍然存在,请让我知道。

eufgjt7s

eufgjt7s3#

这似乎是Maven内部的一个错误。我也多次面对这个问题。即使当类存在但根据错误没有被检测到时,执行以下操作对我也是有效的。

方案一:删除源包中的Java FILE类。

1.创建代码备份,并将Java文件从源包中删除。
1.通过使用与您遇到错误时相同的Java Class代码创建新文件,将该文件添加回源包。

解决方案II:将Java FILE类从一个源包移到另一个源包。

1.将Java FILE类从一个源包移动到另一个源包。
1.然后将Java FILE类放回其原始包。
我希望这两个解决方案中的任何一个都会有帮助。

uurv41yg

uurv41yg4#

我的经验是:

  • 我的Eclipse项目一直运行正常
  • 有时,它会给我相同的错误消息
  • 我所做的只是删除“目标”中的所有文件,然后重新编译。看起来不错。

相关问题