没有这样的bean定义并且不可能创建bean

时间:2014-06-23 22:26:50

标签: java spring hibernate jpa

我正在开发包含4层的Web应用程序。 我使用spring MVC JPA / HIBERNATE框架。 问题是我无法在 creerBilanImpl 类中对 opmanimpl 字段进行自动装配 我没有得到这样的bean定义异常。 我需要帮助!!

ConstruireBilanImpl

@Service
public class ConstruireBilanImpl implements ConstruireBilan {

    @Autowired(required=true)
    @Qualifier("OperationManagerImpl")
    private  OperationManager opmanimpl ;

    private float immobilisations_corporelles=sommeComptes(1266,1322);

    public ConstruireBilanImpl(){};
    public void setOpmanimpl(OperationManagerImpl x){opmanimpl=x;}

    public float sommeComptes(int... doubles){
        if (this.opmanimpl==null){System.out.println("BAAAAAAAAAAAAAADDDDDDDDDD");}
        return opmanimpl.sommeComptes(doubles);


    }

    public float getImmobilisations_corporelles(){return immobilisations_corporelles;}
    public void setImmobilisations_corporelles(float x){immobilisations_corporelles=x;}
}

OperationManagerImpl

@Repository
public class OperationManagerImpl implements OperationManager {

    @PersistenceContext(unitName="ERP")
        private EntityManager em;

        public OperationManagerImpl(){};

        public void setEm(EntityManager emm){em=emm;}


        public void creerOperation(opération op) {
            if (em!=null){System.out.print(" manager de bean initialisé");}

              em.persist(op);



            System.out.println("dou5oul---------2");}

        public opération rechercherOperation(int clé){
            opération oper=em.find(opération.class,clé);
            return oper;

        }

        public void supprimerOperation(int clé)
        {
            opération oper=em.find(opération.class,clé);
            em.remove(oper);
        }


        public void modifierId(int clé,int nv){
            opération oper=em.find(opération.class,clé);
            oper.setIdop(nv);
            em.flush(); 
        }
        public void modifierJournal(int clé,char j){
            opération oper=em.find(opération.class,clé);
            oper.setJournal(j);
            em.flush(); 

        }
        public void modifierCompte(int clé,int nvc){
            opération oper=em.find(opération.class,clé);
            oper.setCompte(nvc);
            em.flush(); 
        }
        public void modifierLibellé(int clé,String nvl ){
            opération oper=em.find(opération.class,clé);
            oper.setLibellé(nvl);
            em.flush(); 

        }
        public void modifierDate(int clé,Date nvd){

            opération oper=em.find(opération.class,clé);
            oper.setDateop(nvd);
            em.flush(); 
        }
        public void modifierDr(int clé,float nvdr){

            opération oper=em.find(opération.class,clé);
            oper.setDr(nvdr);
            em.flush(); 
        }
        public void modifierCr(int clé,float nvcr){

            opération oper=em.find(opération.class,clé);
            oper.setCr(nvcr);
            em.flush(); 
        }


        public List<opération> listeOpération() {
            String ss="select op from opération op";
            Query req= em.createQuery(ss);

            return req.getResultList();
        }


        public void SupprimerOpération(int ref){

            opération op=em.find(opération.class, ref);
            em.remove(op);

        }

        public float sommeComptes(int... doubles) {
            String liste= new String();
            String x;
            for (int d : doubles) {
                x=Integer.toString(d);
                x=x.concat(",");
                liste=liste.concat(x);
            }
        liste=liste.substring(0, liste.length()-1);
        String cc =" select sum(op.cr) from opération op ";
         System.out.println(cc);
            Query req= em.createQuery(cc);
            Number somme=(Number)req.getSingleResult();
            return somme.floatValue();

        }

    }

栈跟踪

  juin 24, 2014 2:34:29 AM org.apache.catalina.core.StandardContext listenerStart
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.BeanCreationException: Error creating bean with name 'operationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ensi.erp.metier.ConstruireBilan com.ensi.erp.controllers.OperationController.bil; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'construireBilanImpl' defined in file [C:\Users\housseminfo\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ERP_PCD\WEB-INF\classes\com\ensi\erp\metier\ConstruireBilanImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.ConstruireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4961)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    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.ensi.erp.metier.ConstruireBilan com.ensi.erp.controllers.OperationController.bil; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'construireBilanImpl' defined in file [C:\Users\housseminfo\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ERP_PCD\WEB-INF\classes\com\ensi\erp\metier\ConstruireBilanImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.ConstruireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 23 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'construireBilanImpl' defined in file [C:\Users\housseminfo\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ERP_PCD\WEB-INF\classes\com\ensi\erp\metier\ConstruireBilanImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.ConstruireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1049)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:873)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:815)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 25 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ensi.erp.metier.ConstruireBilanImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:121)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
    ... 37 more
Caused by: java.lang.NullPointerException
    at com.ensi.erp.metier.ConstruireBilanImpl.sommeComptes(ConstruireBilanImpl.java:67)
    at com.ensi.erp.metier.ConstruireBilanImpl.<init>(ConstruireBilanImpl.java:22)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 39 more

根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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->
        <bean id="datasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/dberp"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultDataSource" ref="datasource"></property>
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
<property name="persistenceUnitName" value="ERP"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>


<bean name="operationmanager"
class="com.ensi.erp.dao.OperationManagerImpl"/>

<bean id="opser" class="com.ensi.erp.metier.gererOperationImpl">
<property name="opmanimpl" ref="operationmanager"></property>
</bean>


  <!-- <bean name="opmanimpl2"
class="com.ensi.erp.dao.OperationManagerImpl"/>

<bean id="bilan" class="com.ensi.erp.metier.ConstruireBilanImpl">
<property name="opmanimpl" ref="opmanimpl2"></property>
</bean>-->


 <tx:annotation-driven transaction-manager="transactionManager"/>
 <context:annotation-config/>

<context:component-scan base-package="com.ensi"/>
</beans>

2 个答案:

答案 0 :(得分:1)

您尚未将 application-context.xml 关联到 web.xml 文件中。

这就是问题所在。

检查弹簧配置文件的 web.xml 中是否有任何条目

或者:

您可能有两个配置文件,例如:

  1. 弹簧servlet.xml中
  2. spring-security.xml
  3. 因此,在一个中定义的bean无法在另一个中找到它们。

    所以你应该在 spring-security.xml 中尝试类似<import resource="spring-servlet.xml"/>的内容。

答案 1 :(得分:0)

默认的bean名称是小写的。而不是

@Autowired(required=true)
@Qualifier("OperationManagerImpl")
private  OperationManager opmanimpl ;

使用

@Autowired(required=true)
@Qualifier("operationManagerImpl")
private  OperationManager opmanimpl ;

请注意,bean的名称是“ operationManagerImpl ”。

编辑:使用此类型的自动装配在构造函数中访问opmanimpl

@Service
public class ConstruireBilanImpl implements ConstruireBilan {

    private  OperationManager opmanimpl ;

    @Autowired
    public ConstruireBilanImpl(@Qualifier("operationManagerImpl") OperationManager opmanimpl) {
        this.opmanimpl = opmanimpl;
    }