第三阶段:Spring事务+完成Entity(Person)相对应的Service层和Dao层+test
1.添加Spring声明式事务配置tx:advice和aof:config
在ApplicationContext-db.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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 id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<tx:advice transaction-manager="transactionManager" id="tx">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
<tx:method name="update*" read-only="false"/>
<tx:method name="del*" read-only="false"/>
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.s2sh.struts2.service.impl.*.*(..))" id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
<!-- 声明式事务 -->
</beans>
2.添加Dao层和Service层代码,然后写出相应的配置文件
PersonDao:
package cn.itcast.s2sh.struts2.dao;
import java.io.Serializable;
import cn.itcast.s2sh.struts2.domain.Person;
public interface PersonDao{
public void savePerson(Person entity);
public Person getPersonById(Serializable id);
}
PersonDaoImpl:
package cn.itcast.s2sh.struts2.dao.impl;
import java.io.Serializable;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.itcast.s2sh.struts2.dao.PersonDao;
import cn.itcast.s2sh.struts2.domain.Person;
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{
@Override
public void savePerson(Person entity) {
this.getHibernateTemplate().save(entity);
}
@Override
public Person getPersonById(Serializable id) {
return (Person)this.getHibernateTemplate().load(Person.class, id);
}
}
PersonService:
package cn.itcast.s2sh.struts2.service;
import java.io.Serializable;
import cn.itcast.s2sh.struts2.domain.Person;
public interface PersonService {
public void savePerson(Person entity);
public Person getPersonById(Serializable id);
}
PersonServiceImpl
package cn.itcast.s2sh.struts2.service.impl;
import java.io.Serializable;
import cn.itcast.s2sh.struts2.dao.PersonDao;
import cn.itcast.s2sh.struts2.domain.Person;
import cn.itcast.s2sh.struts2.service.PersonService;
public class PersonServiceImpl implements PersonService{
private PersonDao personDao;
@Override
public void savePerson(Person entity) {
personDao.savePerson(entity);
}
@Override
public Person getPersonById(Serializable id) {
return personDao.getPersonById(id);
}
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
}
ApplicationContext-Person.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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 id="personDao" class="cn.itcast.s2sh.struts2.dao.impl.PersonDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="personService" class="cn.itcast.s2sh.struts2.service.impl.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao"/>
</property>
</bean>
</beans>
3.写savePerson方法,测试事务添加正确
package cn.itcast.s2sh.test;
import org.junit.Before;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BaseSpring {
public static ApplicationContext context;
@Before
public void startSpring(){
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
package cn.itcast.s2sh.test;
import org.junit.Test;
import cn.itcast.s2sh.struts2.domain.Person;
import cn.itcast.s2sh.struts2.service.PersonService;
public class savePersonTest extends BaseSpring{
@Test
public void savePerson(){
PersonService personService = (PersonService)context.getBean("personService");
Person person = new Person();
person.setName("张三");
personService.savePerson(person);
}
}
测试通过了则说明这一步的搭建成功完成。第四阶段:struts2+spring部分配置+test
1.新建Action,并完成方法
package cn.itcast.s2sh.struts2.action;
import cn.itcast.s2sh.struts2.domain.Person;
import cn.itcast.s2sh.struts2.service.PersonService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class PersonAction extends ActionSupport{
private PersonService personService;
private Person person;
public String savePerson(){
Person person = new Person();
person.setName("王二");
personService.savePerson(person);
return null;
}
public String getPersonById(){
Person person = personService.getPersonById(1L);
//ServletActionContext.getRequest().setAttribute("person", person);
ActionContext.getContext().getValueStack().push(person);
return "index";
}
public PersonService getPersonService() {
return personService;
}
public void setPersonService(PersonService personService) {
this.personService = personService;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
2.在Spring的配置文件ApplicationContext-person.xml中添加Action的配置
ApplicationContext-Person.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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 id="personDao" class="cn.itcast.s2sh.struts2.dao.impl.PersonDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="personService" class="cn.itcast.s2sh.struts2.service.impl.PersonServiceImpl">
<property name="personDao">
<ref bean="personDao"/>
</property>
</bean>
<bean id="personAction" class="cn.itcast.s2sh.struts2.action.PersonAction" scope="prototype">
<property name="personService">
<ref bean="personService" />
</property>
</bean>
</beans>
写完后把它引入到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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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">
<import resource="applicationContext-db.xml"/>
<import resource="applicationContext-person.xml"/>
</beans>
3.写sturts的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 配置文件改了以后不用重新启动 -->
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="struts" namespace="/" extends="struts-default">
<action name="person_*" class="personAction" method="{1}">
<result name="index">index.jsp</result>
</action>
</package>
</struts>
在这里我们需要注意一点:class="personAction",并不是完整的PersonAction路径,而完整的PersonAction路径配置在Spring的配置文件ApplicationContext-Person中,这里的personAction是Spring配置文件中的id值。通过这样配置,Spring就可以管理Action的类了,由Spring实例化。
这是我们在根目录下有一个页面index.jsp
4.写Web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>ItcastOAZD1</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这里的配置非常重要。
Tomcat启动后,首先要加载web.xml,通过监听器ContextLoaderListener来实现Tomcat容器和Spring容器的整合。然后配置context-param:路径指向applicationContext.xml最后是两个重要的filter,这两个filter的顺序不可以变化。一定要先OpenSessionInViewFilter,再Struts2核心过滤器StrutsPrepareAndExecuteFilter。
OpenSessionInViewFilter是为了防止懒加载造成的异常。页面要读取懒加载的数据时一定要保证session开启.session一关闭就出错了。而Spring管理事务,开启Session从Service层开始的,返回到Service就关闭了。所以需要OpenSessionInViewFilter来提前开启Session,提前到struts之前。
5.测试整个搭建过程是否正确
在网址中输入:http://localhost:8080/项目名称/person_getPersonById.action,看看返回的页面是否是index.jsp即可。
以上两篇博客内容就是我总结出来的从底层一点一点搭建SSH的过程。希望能对初学者有帮助。