1
當我嘗試運行我的程序(:沒有定義名爲'org.springframework.orm.hibernate4.LocalSessionFactoryBean'的bean)時,我得到一個NoSuchBeanDefinitionException然而這個bean已經在Beans2.xml文件中定義。這些課程列在下面。謝謝。NoSuchBeanDefinitionException當使用Spring和休眠時
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>
我得到消息「Attribute ref不允許出現在元素bean中 –