SSH 组建轻量级架构 (二) -- 构建步骤
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> 

<!-- 支持事务标记的Bean定义 tx 标记要配合上面的 smlns:ts 和 xsi. 使用 -->
<!-- tx:annotation-driven 检查Bean是否支持事务,当检查到DAO类时的 @Transactional 标注,就自动加入事务管理功能。 -->
<!-- transaction-manager="transactionManager" 指明使用的是 transactionManager 这个 bean 中定义的事务管理器 -->
<!-- proxy-target-class="true",属性值指定 代理的DAO 是 接口 还是 类 ,true表示类,false或默认表示是接口 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> 


<!-- 1、利用 MyEclipse 创建 SessionFactory 的向导 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

<!-- 2、生成基于 Spring Hibernate Template 的 DAO -->
<!-- 持久层Bean -->
<bean id="EcConsignmentDAO" class="springdao.EcConsignmentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="EcReckoningDAO" class="springdao.EcReckoningDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</beans>
import org.springframework.transaction.annotation.Transactional;
@Transactional 
public class EcReckoningDAO extends HibernateDaoSupport
{

..
<!-- 用户业务Bean -->
<bean id="ecAccountBiz" class="biz.EcAccountBiz">
<!-- 注入持久层Bean -->
<property name="ecAccountLevelDAO">
<ref local="EcAccountLevelDAO"/>
</property>
<property name="ecAccountDAO">
<ref local="EcAccountDAO"/>
</property>
<property name="ecAccountStatusDAO">
<ref local="EcAccountStatusDAO"/>
</property>
</bean>
public class EcAccountBiz
{ 
/**//* 定义属性 */
private EcAccountDAO ecAccountDAO;
private EcAccountLevelDAO ecAccountLevelDAO;
private EcAccountStatusDAO ecAccountStatusDAO;

/**//* getXXX() and setXXX() */






/** *//**
* 根据 Id 获得等级信息
* @param levelId 等级ID
* @return 等级信息
*/
public EcAccountLevel getLevel(int levelId)
{
List list = getEcAccountLevelDAO().findAll();
EcAccountLevel level = null; 
if (list.size() > 0)
{
level = (EcAccountLevel)list.get(0);
}
return level;
}
<action-mappings >
<action
attribute="regAccountForm"
input="/join/index.jsp"
name="regAccountForm"
path="/regAccount"
scope="request"
type="com.yourcompany.struts.action.RegAccountAction">
<forward
name="success"
path="/join/regsuccess.jsp"
redirect="true" />
<forward name="fail" path="/join/index22.jsp" />
</action>





.
<!-- 以下写于配置文件尾 -->
<!-- 加入了 Spring 的代理请求处理器(需要写在message-resources上方?) -->
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
<!-- 插件方式 是加载 Spring -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
</plug-in> 

public class RegAccountAction extends Action
{
private EcAccountBiz ecAccountBiz;

/** *//** property accessors */
public EcAccountBiz getEcAccountBiz()
{
return ecAccountBiz;
}

public void setEcAccountBiz(EcAccountBiz ecAccountBiz)
{
this.ecAccountBiz = ecAccountBiz;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException
{
..
EcAccountLevel level = getEcAccountBiz().getLevel(1);
EcAccountStatus status = getEcAccountBiz().getStatus(1);
..
<!-- Struts Action 类 -->
<bean name="/regAccount"
class="com.yourcompany.struts.action.RegAccountAction">
<!-- 注入业务逻辑Bean -->
<property name="ecAccountBiz">
<ref local="ecAccountBiz"/>
</property>
</bean>
旧版的分割线
Spring 配置文件 applicationContext.xml
<!-- 1、利用 MyEclipse 创建 SessionFactory 的向导 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

<!-- 2、生成基于 Spring Hibernate Template 的 DAO -->
<bean id="EcConsignmentDAO" class="springdao.EcConsignmentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="EcReckoningDAO" class="springdao.EcReckoningDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
1、DAO 类加入标注,其开头部分的代码如下所示: 
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class StudentDAO extends HibernateDaoSupport {


2、修改 Spring 配置文件 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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> 

<!-- 支持事务标记的Bean定义 tx 标记要配合上面的 smlns:ts 和 xsi. 使用 -->
<!-- tx:annotation-driven 检查Bean是否支持事务,当检查到DAO类时的 @Transactional 标注,就自动加入事务管理功能。 -->
<!-- transaction-manager="transactionManager" 指明使用的是 transactionManager 这个 bean 中定义的事务管理器 -->
<!-- proxy-target-class="true",属性值指定 代理的DAO 是 接口 还是 类 ,true表示类,false或默认表示是接口 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> 

..
<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</beans>
public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDAO dao = (StudentDAO)ctx.getBean("StudentDAO");
Student user = new Student();
user.setPassword("密码");
user.setUsername("spring 2 标注事务代理测试");
user.setAge(200);
dao.save(user); 


/**//* //原始 Hibernate ,需要手动加入事务功能,并控制会话的开关
StudentDAO dao = new StudentDAO();
Transaction tran = dao.getSession().beginTransaction(); 
Student bean = new Student();
bean.setUsername("张三");
bean.setPassword("1234");
bean.setAge(100); 
dao.save(bean);
tran.commit();
dao.getSession().close();
*/
}
最简单的事务处理办法,可以最大化的利用 JDK 5 的标注功能并大大减少编码的难度。
在 XML 文件尾加入:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>
<action
path="/getGallery"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="returnGallery" path="/my/gallery.jsp" />
</action>
通过对 Struts.action.path 和 Spring.bean.name 的 name 进行匹配, 实现软接口和AOP的准备
<bean
name="/getGallery"
class="com.yourcompany.struts.action.GetGalleryAction">
</bean>
浙公网安备 33010602011771号