最近剛剛開始做SSH三大框架整合,搭建了好幾天的開發環境,就卡在了Spring與Hibernate
整合上,
TestService.java
package com.zyf.test;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.zyf.pj.domain.User;
@Service
public class TestService {
@Resource
private SessionFactory sessionFactory;
@Transactional
public void saveTwoUsers() {
Session session = sessionFactory.getCurrentSession();
session.save(new User());
session.save(new User());
}
}
TestSpring.java
package com.zyf.test;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
private ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
//測試sessionFactory
@Test
public void testSessionFactory() throws Exception{
SessionFactory sessionFactory =(SessionFactory)ac.getBean("sessionFactory");
System.out.println(sessionFactory.openSession());
}
//測試事物
@Test
public void testSaveUsers(){
TestService ts=(TestService) ac.getBean("TestService");
ts.saveTwoUsers();
}
}
Hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- 數據連接信息
jdbc:mysql:///pj
com.mysql.jdbc.Driver
root
abc123
-->
org.hibernate.dialect.MySQL5InnoDBDialect
<!-- 其他配置信息 -->
true
update
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 自動掃描與裝配bean -->
/context:component-scan
<!-- 導入外部的properties配置文件 -->
<!-- 配置數據庫連接池 -->
<!-- =========== 數據庫連接信息 =========== -->
<!-- =========== 連接池的管理配置 =========== -->
<!--初始化時獲取三個連接,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
<!--連接池中保留的最小連接數。Default: 3 -->
<!--連接池中保留的最大連接數。Default: 15 -->
<!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
<!-- 控制數據源內加載的PreparedStatements數量。如果maxStatements與maxStatementsPerConnection均為0,則緩存被關閉。Default: 0 -->
<!--maxStatementsPerConnection定義了連接池內單個連接所擁有的最大緩存statements數。Default: 0 -->
<!--最大空閒時間,1800秒內未使用則連接被丟棄。若為0則永不丟棄。Default: 0 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置聲明式的事務管理(采用基於注解的方式) -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
TestSpring.java
package com.zyf.test;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
private ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
//測試sessionFactory
@Test
public void testSessionFactory() throws Exception{
SessionFactory sessionFactory =(SessionFactory)ac.getBean("sessionFactory");
System.out.println(sessionFactory.openSession());
}
//測試事物
@Test
public void testSaveUsers(){
TestService ts=(TestService) ac.getBean("TestService");
ts.saveTwoUsers();
}
}
對TestSpring進行單元測試,第一個方法正常執行,第二個方法報錯No bean named 'TestService' is defined,並且在數據庫中自動生成test_users表,困擾了好久了,麻煩大神指點一下!!!!
你用的是單元測試,獲取的配置文件applicationContext中沒有配置TestService類的bean。
解決辦法:配置bean.
<bean id="TestService" class="com.zyf.test.TestService">
<property name="sessionFactory " ref="sessionFactory"></property>
</bean>
還可以用SpringJunit4Java進行單元測試,這樣使用注解定義的Bean可以不用在applicatonContext中配置了,注解@Autowired就能生效了。
你需要保證TestService的包在注解掃描路徑下,示例代碼:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.bohui.css.service.IUserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml","classpath:spring-servlet.xml"})
public class TestSpring {
@Autowired
TestService testService ;
@Test
public void testSaveUsers(){
testService.saveTwoUsers();
}
}