---
title: "Spring管理Stucts和Hibernate"
author: "zhou_wu"
date: "2011-03-16T22:54:45Z"
tags: []
categories: ["企业架构"]
slug: "spring-stucts-hibernate"
draft: false
lastmod: "2011-03-16T22:54:45Z"
id: 964797
url: "https://leesmart.iteye.com/blog/964797"
---

项目做完了,总结一下.该项目是利用SSH技术实现的.下面来谈谈Spring是如何管理Struts2和hibernate的.

一:

   先说说Spring是如何运作的吧. 在tomcat启动的时候, 先会在web.xml中加载两个句话, 

Web.xml代码  

```xml
<context-param>   
  
   <param-name>contextConfigLocation</param-name>   
  
   <param-value>classpath:applicationContext.xml</param-value>   
  
</context-param>  

<context-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath:applicationContext.xml</param-value>

</context-param>
```
 

 

```xml
<listener>   
  <listener-class>   
     org.springframework.web.context.ContextLoaderListner   
   </listener-class>   
</listener>  

<listener>
  <listener-class>
     org.springframework.web.context.ContextLoaderListner
   </listener-class>
</listener> 
```

在这里,ContextLoaderListener继承了ServletContextListener.因此,Spring会根据contextConfigLocation所指的Applicationcontext.xml自动装配xml文件中所用到的bean对象.

 

在applicationcontext.xml中bean的定义如下

 

Applicationcontext.xml代码  

```xml
<bean id="user" class="com.test.User" abstract="false" parent="people" scope="prototype">   
   <property name="dept" ref="dept"  ></property>   
</bean>   
   

<bean id="user" class="com.test.User" abstract="false" parent="people" scope="prototype">
   <property name="dept" ref="dept"  ></property>
</bean>
  
```

当id中需要包含斜杠"/" 等特殊字符时,要将id换乘name的同等的. 还有一个特别说明的是,这里的id和name的值必须是唯一的. 

 

 

二: Spring是如何管理hibernate的.以及Spring管理的事务.

 

   Spring加载之后,会根据applicationcontext.xml文件中的配置来装配实例. 

 

   

Java代码  

```xml
<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
   <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>   
    </bean>  

<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>
```
  他会根据hibernate.cfg.xml所配置的加载hibernate配置信息.并实例化sessionFactory.并且如果Dao要受Spring管理, 那么此Dao类需要继承HibernateDaoSupport.

 

Spring提供的事务管理有两种方式,编程式的和申明式的. 编程式的繁琐,我一般不喜欢用.最喜欢的一种是利用Spring封装的aspectJ, 利用Aop来管理事务很方便的. 

 

首先创建一个事务管理器

 

Java代码  

```xml
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
    <property name="sessionFactory" ref="sessionFactory">   
    </property>   
</bean>  

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory">
    </property>
</bean> 
```

声明一个通知, 并声明此通知受某一个事务管理器管理

 

  

Java代码  

```xml
<tx:advice id="txAdvice" transaction-manager="transactionManager">   
<tx:attributes>   
   <tx:method name="save*" />   
   <tx:method name="*" readonly="true" />   
</tx:attributes>   
</tx:advice>  

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
   <tx:method name="save*" />
   <tx:method name="*" readonly="true" />
</tx:attributes>
</tx:advice> 
```

上面的一段话说明了txAdvice受transactionManager事务管理器管理, 并且管理save打头的方法. 其余的都不使用事务管理.

 

最后利用advisor配置advice的切入点.

 

 

Java代码  

```xml
<aop:config>   
  <aop:advisor advice-ref="txAdvice" pointcut="execution(pulic * *..*.service.*.*(..))">   
   </aop:advisor>   
</aop:config>  

<aop:config>
  <aop:advisor advice-ref="txAdvice" pointcut="execution(pulic * *..*.service.*.*(..))">
   </aop:advisor>
</aop:config> 
```

 

这样, 只要经过service层就会进入事务管理.