2017-05-26 7 views
1

私のプログラムを実行しようとするとNoSuchBeanDefinitionExceptionが発生します( 'org.springframework.orm.hibernate4.LocalSessionFactoryBean'という名前のBeanは定義されていません)。BeansはBeans2.xmlファイルで定義されています。クラスは以下の通りです。ありがとう。SpringとHibernateを使用する場合NoSuchBeanDefinitionException

PieceHibernateDAO.java

package sample.spring.chapter01; 

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.commons.csv.CSVFormat; 
import org.apache.commons.csv.CSVParser; 
import org.apache.commons.csv.CSVRecord; 
import org.hibernate.SessionFactory; 
import org.springframework.core.io.Resource; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Repository; 

import org.springframework.transaction.TransactionStatus; 
//import org.springframework.transaction.TransactionCallback; 
//import org.springframework.transaction.TransactionTemplate; 



@Repository(value="PieceHibernateDAO") 
public class PieceHibernateDAO { 

    @Autowired 
    private SessionFactory sessionFactory; 


    public int createPieceHibernate(final Piece piece) { 
     sessionFactory.getCurrentSession().save(piece); 
     return piece.getpieceid(); 
    } 

    //public Piece getPiece 


    public void fill(Resource resource) throws FileNotFoundException, IOException{ 
     InputStream is=resource.getInputStream(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is));    


     CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(br); 

     for (CSVRecord record:parser) { 
      String ref=record.get("référence ascensoriste").trim(); 
      String asc=record.get("ascensoriste").trim(); 
      //String desc=record.get("Description article").trim();     
      String prix=record.get("Pv"); 
      String category=record.get("Familie").trim(); 
      //System.out.println(category); 
      Piece lift_comp=new Piece(); 




      lift_comp.setasc(asc); 
      //lift_comp.setdesc(desc); 
      lift_comp.setref(ref); 
      lift_comp.setprice(prix); 
      lift_comp.settype(category); 
      lift_comp.setinfo(); 
      //listp.add(lift_comp); 
      int cpid=lift_comp.getcompanyid(asc); 
      lift_comp.setcompanyid(cpid); 

      //sessionFactory.getCurrentSession().save(lift_comp); 
      createPieceHibernate(lift_comp); 
     } 


    } 



} 

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

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="sample.spring" /> 
    </bean> 

    <bean class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close" id="dataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/liftcomp_db" /> 
     <property name="username" value="root" /> 
     <property name="password" value="8K887h))00" /> 
    </bean> 


<bean id="PieceHibernateDAO" 
class="sample.spring.chapter01.PieceHibernateDAO"> 
     <property name="sessionFactory" ref="org.springframework.orm.hibernate4.LocalSessionFactoryBean"/> 
    </bean> 



    <bean id="PieceDAO" class="sample.spring.chapter01.PieceDAO"> 
     <property name="listp" ref="Piece"></property>    
    </bean> 
    <bean id = "Company" class = "sample.spring.chapter01.Company"/> 
    <bean id = "Family" class = "sample.spring.chapter01.Family"/> 
    <bean id = "Piece" class = "sample.spring.chapter01.Piece"/> 
</beans> 

答えて

1

あなたはID sessionFeactoryを持つBeanを作成します。 refの値である必要があります。属性:

<property name="sessionFactory" ref="sessionFactory"/> 

これはBeanのIDであるためです。

+0

「属性refは要素Beanに表示できません。 –

関連する問題