2014-06-23 41 views
0

我正在開發包含4層的web應用程序。 我使用Spring MVC JPA/HIBERNATE框架。 問題是我不能自動裝入字段opmanimplcreerBilanImpl類 並且我沒有得到這樣的bean定義異常。 我需要幫助!沒有這樣的bean定義和不可能創建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> 
+0

請發表您的'根context.xml'文件和web.xml'的'相關部分。 –

+0

完成我發佈了root-context和web.xml文件 – housseminfo

回答

1

您尚未連結應用程序的context.xml到您的的web.xml文件

這是問題所在。

檢查,如果你在你的Spring配置文件

或您的web.xml中的任何條目:

您可以有兩個配置文件,例如:

  1. 春天-servlet.xml
  2. spring-security.xml

因此,在其中定義的bean無法在另一箇中找到這些bean。

所以你應該試試<import resource="spring-servlet.xml"/>這樣的spring-security.xml

+0

我做到了,你可以和我一起驗證! <的context-param> \t \t​​contextConfigLocation的 \t \t 類路徑*:根context.xml中 \t housseminfo

+0

你的配置文件的名稱爲**根上下文.xml **? –

+0

是的,我的配置文件名爲root-context.xml。 ididn't have spring-security.xml – housseminfo

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; 
    } 
+0

但類的名稱是OperationManagerImpl,大寫,而不是像您說的那樣是什麼? – housseminfo

+0

在xml文件中有'@Repository public class OperationManagerImpl'和''。 Spring將自動創建一個名爲OperationManagerImpl的bean,名爲'operationManagerImpl',而不是'OperationManagerImpl',就像'@ Qualifier'中的一樣。 –

+0

如果您嘗試在'ConstruireBilanImpl'的默認構造函數中使用'opmanimpl',它將不起作用。 Spring將在現有實例中注入opmanimpl,而不是現有的實例。 –

相關問題