(三)整合SSH测试项目
整合struts 和 spring
预期:如果可以在action中能够正确调用service里面的方法执行并返回到一个页面中;那么我们认定struts和spring的整合是成功的。
编写JUnit测试类,测试spring加载是否正确:
public class TestMerge {
ClassPathXmlApplicationContext ctx;
@Before
public void loadCtx() {
ctx= new ClassPathXmlApplicationContext("applicationContext.xml"); }
@Test
public void testSpring() {
TestService ts = (TestService)ctx.getBean("testService");
ts.say();
}
编写 TestService 接口 和实现类 TestServiceImpl
package cn.itcast.test.service; public interface TestService {
public void say(); }
package cn.itcast.test.service.impl; import org.springframework.stereotype.Service; import cn.itcast.test.service.TestService; @Service("testService")
public class TestServiceImpl implements TestService { @Override
public void say() {
System.out.println("service say haha ");
} }
在applicationContext.xml中添加bean扫描配置信息;这边使用导入配置文件的方式配置。
①首先在cn.itcast.test.conf中建立test-spring.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 扫描service -->
<context:component-scan base-package="cn.itcast.test.service.impl"></context:component-scan>
</beans>
②将test-spring.xml导入到applicationContext.xml中如下:
<!-- 引入外部spring配置文件 --> <!-- <import resource="classpath:cn/itcast/test/conf/test-spring.xml"/>使用通配符 --> <import resource="classpath:cn/itcast/*/conf/*-spring.xml"/>
编写TestAction类
public class TestAction extends ActionSupport {
@Resource
TestService testService; public String execute(){
testService.say();
return SUCCESS;
}
}
在test的conf文件夹下新建test-struts.xml中配置TestAction :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="test-action" namespace="/" extends="struts-default">
<action name="test_*" class="cn.itcast.test.action.TestAction" method="{1}">
<result name="success">/WEB-INF/jsp/test/test.jsp</result>
</action>
</package> </struts>
将test-struts.xml导入到struts.xml文件中。
<include file="cn/itcast/test/conf/test-struts.xml"></include>
在webRoot目录下新建test/test.jsp
在浏览器中输入:http://localhost:8080/itcastTax/test.action 查看后台是否能输入service中的打印信息。
整合hibernate 和 spring
在applicationContext.xml中配置如下原本在hibernate.cfg.xml中需要配置的信息,在spring中配置后hibernate.cfg.xml 可删除。
1、 配置c3p0数据库连接源:
<!-- 导入外部的properties配置文件 -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="${initialPoolSize}"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
2、db.properties
jdbcUrl=jdbc:mysql://localhost:3306/itcastTax_0406?useUnicode=true&characterEncoding=utf8
driverClass=com.mysql.jdbc.Driver
user=root
password=123456
initialPoolSize=10
maxPoolSize=30
3、 配置sessionFactory,并将dataSource指向c3p0创建的dataSource:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:cn/itcast/nsfw/*/entity/*.hbm.xml</value>
<value>classpath:cn/itcast/test/entity/*.hbm.xml</value>
</list>
</property>
</bean>
编写实体类Person和对应的映射文件Person.hbm.xml:
public class Person implements Serializable {
private String id;
private String name; public Person() { } public Person(String name) {
super();
this.name = name;
} public Person(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
映射文件的头部信息:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="cn.itcast.test.entity.Person" table="person">
<id name="id" type="java.lang.String">
<column name="id" length="32" />
<generator class="uuid.hex" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true" />
</property>
</class>
</hibernate-mapping>
编写完实体映射文件后,用JUnit测试hibernate和spring的整合,在测试用例中启动spring容器的时候将扫描Person类根据其创建数据库表,并在测试时将向表插入一条数据。
测试hibernate,添加一个人员
@Test
public void testHibernate() {
SessionFactory sf = (SessionFactory) ctx.getBean("sessionFactory"); Session session = sf.openSession();
Transaction transaction = session.beginTransaction(); session.save(new Person("人员1"));
transaction.commit();
session.close();
}
配置spring事务管理
<!-- 事务管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="*" rollback-for="Throwable"/>
</tx:attributes>
</tx:advice>
<!-- aop配置被事务控制的类 -->
<aop:config>
<!-- <aop:pointcut id="serviceOperation" expression="bean(*Service)"/> -->
<aop:pointcut id="serviceOperation" expression="execution(* cn.itcast..service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
</aop:config>
【注意:上面的pointcut expression 表示拦截以Service结尾的bean,或者可写成
execution(* cn.itcast..service.impl.*.*(..))】
完善 TestService接口和TestServiceImpl;利用service中的操作来验证上面配置的事务管理是否生效。
测试方法
Dao中
public class TestDaoImpl extends HibernateDaoSupport implements TestDao { @Override
public void save(Person person) { getHibernateTemplate().save(person);
} @Override
public Person findPerson(Serializable id) { return getHibernateTemplate().get(Person.class,id);
}
}
Service中
@Service("testService")
public class TestServiceImpl implements TestService {
@Resource
TestDao testDao; @Override
public void say() {
System.out.println("service say haha ");
} @Override
public void save(Person person) {
testDao.save(person);
int i= 1/0;
} @Override
public Person findPerson(Serializable id) {
save(new Person("test"));
return testDao.findPerson(id);
} }
(三)整合SSH测试项目的更多相关文章
- 使用eclipse整合ssh项目的例子--lljf(1)
最近向自己单独做一个基于ssh的项目,来预习和巩固自己的Java基础.找了一个实际生活中的定做衣服的例子来做一做,放到博客上给大家一起分享学习,后边会持续更新项目编写时候遇到的困难和使用的技术等. 1 ...
- 搭建ssh框架项目(三)
一.创建业务层 (1)创建业务层接口IElecTextService.java package com.cppdy.ssh.service; import com.cppdy.ssh.domain.E ...
- Spring Test+JUnit4整合使用测试ZZJ_淘淘商城项目:day01(RESTful Web Service)
针对整合的Dao层与Service层,在做spring与通用Mapper和分页插件相关测试时比较麻烦.如果只用JUnit测试,需要每次Test方法里初始化一下applicationContext,效率 ...
- Maven项目整合SSH框架
---------------------siwuxie095 Maven 项目整合 SSH 框架 创建 ...
- maven学习记录三——maven整合ssh框架
6 整合ssh框架 6.1 依赖传递 只添加了一个struts2-core依赖,发现项目中出现了很多jar, 这种情况 叫 依赖传递 6.2 依赖版本冲突的解决 1. 第 ...
- .net到Java那些事儿--整合SSH
一.介绍 整体介绍分成两个部分,第一.net转到Java的原因,第二开发SSH时候的环境介绍: .net到Java的原因: .net开发也将近快3年的样子,加上现在的老东家换过 ...
- Maven02——回顾、整合ssh框架、分模块开发、私服
1 回顾 1.1 Maven的好处 节省空间 对jar包做了统一管理 依赖管理 一键构建 可跨平台 应用在大型项目可提高开发效率 1.2 Maven安装部署配置 1.3 Maven的仓库 本地仓库 远 ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- SSH简单项目
这是我学习SSH整合时的一个测试项目,代码比较简单 整个项目实现从数据库中取数据,在页面上显示.项目的结构如下: (1)数据库设计 数据库使用的是student数据库中的一个数据库表grade,表的内 ...
随机推荐
- 使用ZipArchive解压
本文转载至 http://www.apkbus.com/forum.php?mod=viewthread&tid=131390&extra=page%3D1 qqpk360 该用户从未 ...
- 【BZOJ3834】[Poi2014]Solar Panels 分块好题
[BZOJ3834][Poi2014]Solar Panels Description Having decided to invest in renewable energy, Byteasar s ...
- EasyNVR无插件播放HLS/RTMP网页直播方案前端完善:监听表单变动
在上一篇博客中我们表述完了防止提交成功后多余操作提交的一个过程:其中的精髓在于ajax的触发事件的使用. 而这篇博客主要想说明一下如何实时的判断出表单是否发生变化. 问题表述: 在网页前端的开发过程中 ...
- EasyPlayerPro(Windows)流媒体播放器开发之框架讲解
EasyPlayerPro for Windows是基于ffmpeg进行开发的全功能播放器,开发过程中参考了很多开源的播放器,诸如vlc和ffplay等,其中最强大的莫过于vlc,但是鉴于vlc框架过 ...
- UIWebview加载H5界面侧滑返回上一级
一.UIWebview的发现 问题发现:当UIWebview王深层次点击的时候,返回时需要webView执行goBack方法一级一级返回,这样看到的webView只是在该界面执行刷新,并看不到类似iO ...
- iOS开发常用第三方框架
1.网络通信 1.ASIHTTPRequest 这是一个经典的老库,功能完全而强大,但已经停止更新很久了(iOS5.0停止更新,但是我最近看github上这个项目有新改动).在不同iOS版本上略微有一 ...
- 如果这种方式导致程序明显变慢或者引起其他问题,我们要重新思考来通过 goroutines 和 channels 来解决问题
https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/09.3.md 9.3 锁和 sync 包 在一些复杂的程序中,通常通 ...
- linux install beanstalkd
you can instal it via git and then copy systemd script: Step 0. Install git yum install git Step 1. ...
- 如何在Mac的Finder中显示/usr、/tmp、/var等隐藏目录
原文链接: http://blog.csdn.net/yhawaii/article/details/7435918 Finder中默认是不显示/usr./tmp./var等隐藏目录的,通过在终端中输 ...
- Android6.0 旋转屏幕(五)WMS启动应用流程(屏幕方向相关)
一.强制设置方向 1.Activity 如果要强制设置一个Activity的横竖屏可以通过Manifest去设置,跟Activity相关的信息都会保存在ActivityInfo当中. android: ...