简体   繁体   中英

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class

I'm facing a simple problem and seek for help.

Here's the exception message:

Exception encountered during context initialization - cancelling refresh attempt: 

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot 
find class [dao.ProduitImpl] for bean with name 'sybaway' defined in
ServletContext resource [/WEB-INF/spring-beans.xml]; nested exception
is java.lang.ClassNotFoundException: dao.ProduitImpl


GRAVE: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [dao.ProduitImpl] for bean with name 'sybaway' defined in ServletContext resource [/WEB-INF/spring-beans.xml]; nested exception is java.lang.ClassNotFoundException: dao.ProduitImpl
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1208)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:568)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1277)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:844)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:539)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:261)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:192)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: dao.ProduitImpl
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1858)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1701)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:258)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:408)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1200)
    ... 18 more

Here's the factory:

package dao;

import java.util.ArrayList;
import java.util.List;

public class ProduitImpl implements ProduitDAO{

    private List<Produit> produits=new ArrayList<Produit>();

    public List<Produit> getProduits() {
        return produits;
    }

    public void setProduits(List<Produit> produits) {
        this.produits = produits;
    }

    public void init(){
        System.out.println("www.sybaway.com");
    }

    @Override
    public void addProduit(Produit p) {
        p.setIdProduit(new Long(produits.size()+1));
        produits.add(p);
    }

    @Override
    public void deleteProduit(Long id) {
        produits.remove(getProduitById(id));
    }

    @Override
    public Produit getProduitById(Long id) {
        Produit produit=null;
        for(Produit p:produits){
            if(p.getIdProduit().equals(id))
                produit=p;
                break;
        }
        return produit;
    }

    @Override
    public List<Produit> getAllProduit() {
        return produits;
    }

    @Override
    public void updateProduit(Produit p) {

    }

}

Here's my ProduitImplMetier.java :

    package service;

    import java.util.List;

    import dao.Produit;
    import dao.ProduitDAO;

public class ProduitImplMetier implements ProduitMetier {


    private ProduitDAO dao;

    public void setDao(ProduitDAO dao) {
        this.dao = dao;
    }

    @Override
    public void addProduit(Produit p) {
        dao.addProduit(p);
    }

    @Override
    public void deleteProduit(Long id) {
        dao.deleteProduit(id);
    }

    @Override
    public List<Produit> getAllProduit() {
        return dao.getAllProduit();
    }

    @Override
    public Produit getProduitById(Long id) {
        return dao.getProduitById(id);
    }

}

Here's my spring-beans.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-2.5.xsd">


<bean  id="sybaway" init-method="init" class="dao.ProduitImpl" ></bean>

<bean id="sybawayServices" class="service.ProduitImplMetier" >
    <property name="dao" ref="sybaway"  ></property>
</bean>

</beans>

The exception tells clearly what is missing.

It tells you that your Java Virtual Machine (JVM), can't find your DAO Class ProduitImpl.class , in your CLASSPATH.

The CLASSPATH is a "location" (or folder) on your system where your JVM looks for .class files to load them into the JVM memory and execute the code inside them.

In order to "create" .class files from your .java files, you'll need to compile your .java files.

That can be done easily with a build tool like Maven.

With a single Maven command:

mvn compile

You can achieve the described above, Maven will simply take your .java files, like:

ProduitImpl.java

And will create from it ProduitImpl.class file.

Plus Maven will place that .class file in your CLASSPATH for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM