qq1280418636
qq1280418636
2015-05-16 16:25

SSH整合:No bean named 'TestService' is defined

  • 框架整合

最近刚刚开始做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表,困扰了好久了,麻烦大神指点一下!!!!

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐