这是我的 applicationContext.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-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" /><bean
id="StudentDAO" class="org.myeclipse.hibernatespring.StudentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="persistenceLayer"
class="org.myeclipse.hibernatespring.PersistenceLayer"
abstract="false" lazy-init="default" autowire="default"
p:studentDAO-ref="StudentDAO">
</bean></beans>
我的持久层类从 applicationcontext.xml 获得 StudentDAO 的引用
package org.myeclipse.hibernatespring;
public class PersistenceLayer {
private StudentDAO studentDAO;
public StudentDAO getStudentDAO() {
return studentDAO;
}
public void setStudentDAO(StudentDAO studentDAO) {
this.studentDAO = studentDAO;
}
public Student listStudent(Integer id)
{
return studentDAO.findById(id);
}
}
我的 buisnessLogic 课程创建了一个学生
package org.myeclipse.hibernatespring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class BuisnessLogic {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student st=new Student();
st.setId(4);
st.setName("Gaurav");
st.setPer(56.87f);
BeanFactory bf=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
PersistenceLayer pl=(PersistenceLayer) bf.getBean("persistenceLayer");
Student stu=pl.listStudent(2);
System.out.print(stu.getName());
}
}
这是我运行业务逻辑类时遇到的错误
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)
at org.myeclipse.hibernatespring.StudentDAO.getCurrentSession(StudentDAO.java:39)
at org.myeclipse.hibernatespring.StudentDAO.findById(StudentDAO.java:71)
at org.myeclipse.hibernatespring.PersistenceLayer.listStudent(PersistenceLayer.java:15)
at org.myeclipse.hibernatespring.BuisnessLogic.main(BuisnessLogic.java:20)