web-dev-qa-db-ja.com

org.springframework.beans.factory.BeanCreationException:「MyController」という名前のBeanの作成エラー:

春+休止状態のアプリケーションを実行しています。 Tomcatサーバーでアプリケーションを実行すると、いくつかの例外が発生します。

INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Sat May 17 19:51:03 CEST 2014]; parent: Root WebApplicationContext
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/inscription],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public Java.lang.String com.gestEtu.project.controller.CompteController.hello(Java.lang.String,Java.lang.String,Java.lang.String,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public Java.lang.String com.gestEtu.project.controller.HomeController.home(Java.util.Locale,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.gestEtu.project.model.service.CompteServiceImp com.gestEtu.project.controller.CompteController.compteServ; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.gestEtu.project.model.service.CompteServiceImp.setCompteDAO(com.gestEtu.project.model.dao.CompteDAOHib); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteDAOHib' defined in file [C:\Users\zouhair\workspace\.metadata\.plugins\org.Eclipse.wst.server.core\tmp1\wtpwebapps\TESTER\WEB-INF\classes\com\gestEtu\project\model\dao\CompteDAOHib.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.gestEtu.project.model.dao.CompteDAOHib]: No default constructor found; nested exception is Java.lang.NoSuchMethodException: com.gestEtu.project.model.dao.CompteDAOHib.<init>()
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.Java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:482)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.Java:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.Java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.Java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.Java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.Java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.Java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.Java:160)
    at org.Apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.Java:1189)
    at org.Apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.Java:1103)
    at org.Apache.catalina.core.StandardWrapper.load(StandardWrapper.Java:1010)
    at org.Apache.catalina.core.StandardContext.loadOnStartup(StandardContext.Java:4935)
    at org.Apache.catalina.core.StandardContext$3.call(StandardContext.Java:5262)
    at org.Apache.catalina.core.StandardContext$3.call(StandardContext.Java:5257)
    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: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.gestEtu.project.model.service.CompteServiceImp com.gestEtu.project.controller.CompteController.compteServ; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.gestEtu.project.model.service.CompteServiceImp.setCompteDAO(com.gestEtu.project.model.dao.CompteDAOHib); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteDAOHib' defined in file [C:\Users\zouhair\workspace\.metadata\.plugins\org.Eclipse.wst.server.core\tmp1\wtpwebapps\TESTER\WEB-INF\classes\com\gestEtu\project\model\dao\CompteDAOHib.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.gestEtu.project.model.dao.CompteDAOHib]: No default constructor found; nested exception is Java.lang.NoSuchMethodException: com.gestEtu.project.model.dao.CompteDAOHib.<init>()
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.Java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.Java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.Java:289)
    ... 27 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.gestEtu.project.model.service.CompteServiceImp.setCompteDAO(com.gestEtu.project.model.dao.CompteDAOHib); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteDAOHib' defined in file [C:\Users\zouhair\workspace\.metadata\.plugins\org.Eclipse.wst.server.core\tmp1\wtpwebapps\TESTER\WEB-INF\classes\com\gestEtu\project\model\dao\CompteDAOHib.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.gestEtu.project.model.dao.CompteDAOHib]: No default constructor found; nested exception is Java.lang.NoSuchMethodException: com.gestEtu.project.model.dao.CompteDAOHib.<init>()
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.Java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.Java:1017)
    at org.springframework.be   

root-context.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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource" class="org.Apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>com/gestEtu/project/model/bo/Utilisateur.hbm.xml</value>
                <value>com/gestEtu/project/model/bo/Compte.hbm.xml</value>
                <value>com/gestEtu/project/model/bo/Message.hbm.xml</value>
                <value>com/gestEtu/project/model/bo/Groupe.hbm.xml</value>
                <value>com/gestEtu/project/model/bo/Etudiant.hbm.xml</value>
                <value>com/gestEtu/project/model/bo/Fichelecture.hbm.xml</value>
                <value>com/gestEtu/project/model/bo/Document.hbm.xml</value>
                <value>com/gestEtu/project/model/bo/Commentaire.hbm.xml</value>

            </list>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <!-- Transaction Manager -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean> 
</beans>

servlet-context.xml

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.gestEtu.project" />   

</beans:beans>

CompteController.Java

package com.gestEtu.project.controller;

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

import com.gestEtu.project.model.bo.Compte;
import com.gestEtu.project.model.service.CompteServiceImp;

@Controller
public class CompteController {
    @Autowired
    private CompteServiceImp compteServ;

    // @Autowired
    // public CompteController(CompteServiceImp compteServ) {
    // this.compteServ=compteServ;
    // }
    //
    @RequestMapping(value = "/inscription")
    public String hello(
            @RequestParam(value = "login", required = false, defaultValue = "zouhair") String login,
            @RequestParam(value = "password", required = false, defaultValue = "pass") String password,
            @RequestParam(value = "profil", required = false, defaultValue = "Etudiant") String profil,
            Model model) {
        int id = 1;
        Compte compte = new Compte(id, login, password, profil);
        compteServ.ajouterCompte(compte);
        model.addAttribute("inscription", compte);
        return "addCompte";
    }

}

compteService

package com.gestEtu.project.model.service;

import Java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.stereotype.Service;

import com.gestEtu.project.model.bo.Compte;
import com.gestEtu.project.model.bo.Utilisateur;
import com.gestEtu.project.model.dao.CompteDAOHib;

@Service
@Transactional(readOnly = true)
public class CompteServiceImp implements CompteService {

    private CompteDAOHib compteDAO;


    @Autowired
    public void setCompteDAO(CompteDAOHib compteDAO) {
        this.compteDAO = compteDAO;
    }

    @Override
    public void ajouterCompte(Compte compte) {
        compteDAO.ajouterCompte(compte);
    }

    @Override
    public List<Compte> getComptes() {
        return compteDAO.getComptes();
    }

    @Override
    public Compte findCompteByLogin(String login) {

        return compteDAO.findCompteByLogin(login);
    }

    @Override
    public void ajouterUtilisateur(Compte compte, Utilisateur utilisateur) {
        this.ajouterCompte(compte);

    }

    @Override
    public Utilisateur getUtilisateurById(int id) {
        return null;
    }

    @Override
    public List<Utilisateur> getUtilisateurs() {
        return null;
    }

}

誰でもこれを解決するのに役立ちますか?

CompteDAOHib

package com.gestEtu.project.model.dao;

import Java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gestEtu.project.model.bo.Compte;

@Repository
@Transactional(propagation=Propagation.REQUIRED)
public class CompteDAOHib extends HibernateDaoSupport implements CompteDAO {


    public CompteDAOHib(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }

    @Override
    public void ajouterCompte(Compte compte) {
        getHibernateTemplate().saveOrUpdate(compte);
    }

    @Override
    public List<Compte> getComptes() {
        try{
            return (List<Compte>) getHibernateTemplate().find("from compte")
                .iterator();
        }catch (Exception e) {
            return null;
        }
    }

    @Override
    public Compte findCompteByLogin(String login) {
        if ((login == null) || (login == "")) {
            return null;
        } else
            try {
                return (Compte) getHibernateTemplate()
                        .find("from Compte c where c.login = ?", login)
                        .iterator().next();
            } catch (Exception e) {
                return null;
            }
    }

}

CompteServiceコード

package com.gestEtu.project.model.service;

import Java.util.List;

import com.gestEtu.project.model.bo.Compte;
import com.gestEtu.project.model.bo.Utilisateur;


public interface CompteService {

    public void ajouterCompte(Compte compte);

    public List<Compte> getComptes();

    public Compte findCompteByLogin(String login);

    public List<Utilisateur> getUtilisateurs();

    public Utilisateur getUtilisateurById(int id);

    public void ajouterUtilisateur(Compte compte, Utilisateur utilisateur);

}
18
Zouhair Kasmi

スタックトレースからコピー:

BeanInstantiationException:Beanクラスをインスタンス化できませんでした[com.gestEtu.project.model.dao.CompteDAOHib]:デフォルトのコンストラクターが見つかりません。ネストされた例外はJava.lang.NoSuchMethodExceptionです:com.gestEtu.project.model.dao.CompteDAOHib。<init>()

デフォルトでは、Springはデフォルト(引数なし)コンストラクターを呼び出してBeanのインスタンス化を試みます。あなたの場合の問題は、CompteDAOHibの実装にSessionFactory引数を持つコンストラクタがあることです。 @Autowired注釈をコンストラクターに追加することにより、Springは一致するタイプのBean、あなたの場合はSessionFactoryを見つけようとし、コンストラクター引数として提供します。

@Autowired
public CompteDAOHib(SessionFactory sessionFactory) {
    // ...
}
11
matsev

同様のエラーが発生していますが、皆さんと共有したいだけです。多分それは誰かを助けるでしょう。

EntityManagerFactoryを使用してEntityManagerを取得する場合は、必ず以下を使用してください。

<persistence-unit name="name" transaction-type="RESOURCE_LOCAL">

ではなく:

<persistence-unit name="name" transaction-type="JPA">

persistance.xml

プロジェクトをきれいにして再構築してください。

5
Damiani

org.springframework.beans.factory.BeanCreationException:「MyController」という名前のBeanの作成エラー

ライブラリにojdbc14.jarを追加したことを確認してください。

ここではOracle11gを使用していますが、ojdbc14.jarはありません。私に何ができる?

1
Lalchand Mali

@Controllerから@ServiceにCompteControllerに変更し、@ ServiceアノテーションをCompteDAOHibに追加します。それでもこの問題に直面している場合はお知らせください。

1
geddamsatish

例外は問題を明確に示しています。

CompteDAOHib: No default constructor found

SpringがBeanをインスタンス化するには、クラスCompteDAOHibに空のコンストラクターを提供する必要があります。

1
Sajan Chandran

必要な注釈をすべて追加しても何も起こらない場合は、この依存関係をpom.xmlに追加してみてください。同じ問題に直面したため、ここに追加して解決しました。

<dependency>
   <groupId>commons-configuration</groupId>
   <artifactId>commons-configuration</artifactId>
   <version>1.9</version>
</dependency>
0
Houssem Ouali