(三)整合SSH测试项目

整合struts 和 spring

预期:如果可以在action中能够正确调用service里面的方法执行并返回到一个页面中;那么我们认定struts和spring的整合是成功的。

编写JUnit测试类,测试spring加载是否正确:

 1 public class TestMerge {
 2     ClassPathXmlApplicationContext ctx;
 3     @Before
 4     public void loadCtx() {
 5          ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
 6         
 7     }
 8     @Test
 9     public void testSpring() {
10         TestService ts = (TestService)ctx.getBean("testService");
11         ts.say();
12     }

 

编写 TestService 接口 和实现类 TestServiceImpl

1 package cn.itcast.test.service;
2 
3 public interface TestService {
4     public void say();
5     
6 }
 1 package cn.itcast.test.service.impl;
 2 
 3 import org.springframework.stereotype.Service;
 4 
 5 import cn.itcast.test.service.TestService;
 6 
 7 @Service("testService")
 8 public class TestServiceImpl implements TestService {
 9 
10     @Override
11     public void say() {
12         System.out.println("service say haha ");
13     }
14 
15 }

在applicationContext.xml中添加bean扫描配置信息;这边使用导入配置文件的方式配置。

①首先在cn.itcast.test.conf中建立test-spring.xml,里面内容:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 7     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
 8     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
 9     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
10     
11     
12     <!-- 扫描service -->
13     <context:component-scan base-package="cn.itcast.test.service.impl"></context:component-scan>
14 </beans>

②将test-spring.xml导入到applicationContext.xml中如下:

 1 <!-- 引入外部spring配置文件 --> 2 <!-- <import resource="classpath:cn/itcast/test/conf/test-spring.xml"/>使用通配符 --> 3 <import resource="classpath:cn/itcast/*/conf/*-spring.xml"/> 

编写TestAction类

 

1 public class TestAction extends ActionSupport {
2     @Resource
3     TestService testService; 
4 
5     public String execute(){
6         testService.say();
7         return SUCCESS;
8     }
9 }

在test的conf文件夹下新建test-struts.xml中配置TestAction :

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <package name="test-action" namespace="/" extends="struts-default">
 8         <action name="test_*" class="cn.itcast.test.action.TestAction" method="{1}">
 9             <result name="success">/WEB-INF/jsp/test/test.jsp</result>
10         </action>
11     </package>
12 
13 </struts>

将test-struts.xml导入到struts.xml文件中。

 1 <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数据库连接源:

 1     <!-- 导入外部的properties配置文件 -->
 2     <context:property-placeholder location="classpath:db.properties" />
 3 
 4     <!-- 配置c3p0数据源 -->
 5     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
 6         <property name="jdbcUrl" value="${jdbcUrl}"></property>
 7         <property name="driverClass" value="${driverClass}"></property>
 8         <property name="user" value="${user}"></property>
 9         <property name="password" value="${password}"></property>
10         <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
11         <property name="initialPoolSize" value="${initialPoolSize}"></property>
12         <!--连接池中保留的最小连接数。Default: 3 -->
13         <property name="minPoolSize" value="3"></property>
14         <!--连接池中保留的最大连接数。Default: 15 -->
15         <property name="maxPoolSize" value="${maxPoolSize}"></property>
16         <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
17         <property name="acquireIncrement" value="3"></property>
18         <!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->
19         <property name="maxIdleTime" value="1800"></property>
20     </bean>

2、db.properties

1 jdbcUrl=jdbc:mysql://localhost:3306/itcastTax_0406?useUnicode=true&characterEncoding=utf8
2 driverClass=com.mysql.jdbc.Driver
3 user=root
4 password=123456
5 initialPoolSize=10
6 maxPoolSize=30

3、  配置sessionFactory,并将dataSource指向c3p0创建的dataSource:

 1     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 2         <property name="dataSource" ref="dataSource"></property>
 3         <property name="hibernateProperties">
 4             <props>
 5                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
 6                 <prop key="hibernate.show_sql">true</prop>
 7                 <prop key="hibernate.hbm2ddl.auto">update</prop>
 8                 <prop key="javax.persistence.validation.mode">none</prop>
 9             </props>
10         </property>
11         <property name="mappingLocations">
12             <list>
13                 <value>classpath:cn/itcast/nsfw/*/entity/*.hbm.xml</value>
14                 <value>classpath:cn/itcast/test/entity/*.hbm.xml</value>
15             </list>
16         </property>
17     </bean>

编写实体类Person和对应的映射文件Person.hbm.xml:

 1 public class Person implements Serializable {
 2     private String id;
 3     private String name;
 4     
 5     public Person() {
 6 
 7     }
 8     
 9     public Person(String name) {
10         super();
11         this.name = name;
12     }
13 
14     public Person(String id, String name) {
15         this.id = id;
16         this.name = name;
17     }
18     public String getId() {
19         return id;
20     }
21     public void setId(String id) {
22         this.id = id;
23     }
24     public String getName() {
25         return name;
26     }
27     public void setName(String name) {
28         this.name = name;
29     }
30     
31 }

映射文件的头部信息:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 
 5 <hibernate-mapping>
 6     <class name="cn.itcast.test.entity.Person" table="person">
 7         <id name="id" type="java.lang.String">
 8             <column name="id" length="32" />
 9             <generator class="uuid.hex" />
10         </id>
11         <property name="name" type="java.lang.String">
12             <column name="name" length="20" not-null="true" />
13         </property>
14     </class>
15 </hibernate-mapping>

编写完实体映射文件后,用JUnit测试hibernate和spring的整合,在测试用例中启动spring容器的时候将扫描Person类根据其创建数据库表,并在测试时将向表插入一条数据。

测试hibernate,添加一个人员

 1     @Test
 2     public void testHibernate() {
 3         SessionFactory sf = (SessionFactory) ctx.getBean("sessionFactory");
 4         
 5         Session session = sf.openSession();
 6         Transaction transaction = session.beginTransaction();
 7         
 8         session.save(new Person("人员1"));
 9         transaction.commit();
10         session.close();
11     }

  配置spring事务管理

 1 <!-- 事务管理 -->
 2     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 3         <property name="sessionFactory" ref="sessionFactory"></property>
 4     </bean>
 5     <!-- 事务通知 -->
 6     <tx:advice id="txAdvice" transaction-manager="txManager">
 7         <tx:attributes>
 8             <tx:method name="find*" read-only="true"/>
 9             <tx:method name="get*" read-only="true"/>
10             <tx:method name="list*" read-only="true"/>
11             <tx:method name="*" rollback-for="Throwable"/>
12         </tx:attributes>
13     </tx:advice>
14     <!-- aop配置被事务控制的类 -->
15     <aop:config>
16         <!-- <aop:pointcut id="serviceOperation" expression="bean(*Service)"/> -->
17         <aop:pointcut id="serviceOperation" expression="execution(* cn.itcast..service.impl.*.*(..))"/>
18         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
19     </aop:config>

 

【注意:上面的pointcut  expression 表示拦截以Service结尾的bean,或者可写成

execution(* cn.itcast..service.impl.*.*(..))】

完善 TestService接口和TestServiceImpl;利用service中的操作来验证上面配置的事务管理是否生效。

 

测试方法

Dao中

 1 public class TestDaoImpl extends HibernateDaoSupport implements TestDao {
 2 
 3     
 4     @Override
 5     public void save(Person person) {
 6     
 7         getHibernateTemplate().save(person);
 8     }
 9 
10     @Override
11     public Person findPerson(Serializable id) {
12         
13         return getHibernateTemplate().get(Person.class,id);
14     }
15 }

Service中

 1 @Service("testService")
 2 public class TestServiceImpl implements TestService {
 3     @Resource
 4     TestDao testDao;
 5     
 6     @Override
 7     public void say() {
 8         System.out.println("service say haha ");
 9     }
10 
11     @Override
12     public void save(Person person) {
13         testDao.save(person);
14         int i= 1/0;
15     }
16 
17     @Override
18     public Person findPerson(Serializable id) {
19         save(new Person("test"));
20         return testDao.findPerson(id);
21     }
22     
23 }

 

posted on 2016-10-03 09:46  Michael2397  阅读(236)  评论(0编辑  收藏  举报

导航