lorenhood 发表于 2013-2-3 10:35:44

Struts2,Mybatis与Spring事务管理的集成

刚接触Struts2和Spring。Spring的事务管理可以帮助开发,而不需要再写rollback和commit。

刚一开始,所有代码和配置都好了,可是最后调用Action 时,却总是 NullPointException,是一个业务逻辑的Service没有设置值。但是这字段设置了@Autowired的。

终于,通过internet知道,我缺少了Struts2-spring-plugin。

增加plugin的 lib 后,Struts.xml 需要修改。

struts.xml 文件中,
增加 一个 常量定义
<constant name="struts.objFactory" value="spring" />
把 Action 的 class的值 改成 bean ID
如原来的 <Action name="login" class="org.test.LoginAction">....
那么,更后改就是 <Action name="login" class="loginAction">....
OK了,能正常执行了。


稍想一下就明白了其中的道理
Struts2 的 Action 和 Struts1 的Action 是不同的运行方式。
Struts1 从始至终一个Action都只有一个实例,Action中的类变量是线程不安全的。
Struts2 则不同,每一次Action请求,它都会创建一个新的实例,是由 Struts2的Filter负责创建的。
那么 Spring事务管理的容器里的所有Bean,都是由Spring创建的。当你去getBean的时候,它就会返回一个已经装配好的Bean给你。

说到这儿就可以看出,在没有Struts2-spring-plugin的时候,每次都是由Struts2自己new了一个Action来执行请求,那么Action里的成员是未经赋值的,肯定会导致Null引用。

Strut2-spring-plugin包,就是实现了从Spring容器中获取指定类型的bean,再由这个bean复制一个新的实例,再交给Struts2使用,这样就实现了从单例的Bean,每次请求时都是一个新的实例。

那么,再来看一下示例的Spring的配置文件
<?xml version="1.0" encoding="UTF-8"?><!-- DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" --><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:jdbc="http://www.springframework.org/schema/jdbc"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">    <bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">      <property name="location" value="classpath:jdbc.properties" />    </bean>    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >      <property name="driverClassName" value="${jdbc.driver}" />      <property name="url">            <value>${jdbc.url}</value>      </property>      <property name="username">            <value>${jdbc.user}</value>      </property>      <property name="password">            <value>${jdbc.password}</value>      </property>      <property name="maxActive" value="2" />      <property name="initialSize" value="1" />      <property name="maxIdle" value="2" />      <property name="minIdle" value="1" />      <property name="maxWait" value="30000" />    </bean>    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">      <property name="dataSource" ref="dataSource" />      <propertyname="mapperLocations"value="classpath:com.test.dao.*.xml"/>    </bean>    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">      <property name="dataSource" ref="dataSource" />    </bean>    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">      <property name="basePackage" value="com.test.dao" />    </bean>    <context:annotation-config />    <context:component-scan base-package="com.test.Action"/>    <context:component-scan base-package="com.test.business"/>    <aop:aspectj-autoproxy />    <tx:annotation-driven transaction-manager="transactionManager" /></beans>

在web.xml 中,
    <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>            /WEB-INF/classes/SpringConfig.xml      </param-value>    </context-param>    <listener>      <listener-class>            org.springframework.web.context.ContextLoaderListener      </listener-class>    </listener>

那在java代码方面,
Pojo、Mapper,都是由 Mybatis-generator 自动生成的,未做修败。
手工编写的代码的只有业务服务Service和Action
也来个示例
这是个Action
@Controllerpublic class PdoAction extends ActionSupport{    @Autowired    private IService service;    public String execute() throws Exception {      service.insertCard(1L);      return SUCCESS;    }}
最后,再贴个Service
@Servicepublic class CardService implements IService {    @Autowired    private NboAcctinfoMapper acctmapper;    @Autowired    private TCardMapper cardmapper;    @Transactional    @Override    public void insertCard(Long cardid){      //DAO 数据库内容操作,不需要在这个方法内做异常捕获,可以在Action捕获或不捕获。    }}
页: [1]
查看完整版本: Struts2,Mybatis与Spring事务管理的集成