关于Spring与Struts的整合中,如何加载Spring容器:
1)直接在Action中获取:
ApplicationContext contxt = newClassPathXmlApplicationContext("applicationContext.xml");
userBO =(UserBO)contxt.getBean("UserBO");
问题:如果有多个Action的话,会重复的读取applicationContext.xml文件,势必会造成效率的低下,
而且最重要的是Spring是一个容器,多个Action会产生出来多个Spring容器,多个Spring容器如
何去管理Struts和Hibernate,Struts和Hibernate又如何知道它们会被哪一个Spring容器所管
理,这样势必会造成管理的混乱。对于Web项目工程来说,只需一个Spring容器就能管理Struts和
Hibernate了。因此,直接在Action中获取Spring容器是不可取的
2)通过struts-config.xml来使Web容器加载Spring ----使用Spring容器插件的方式
<controller> ----使用控制器加载Spring,告知Struts将要被Spring所管理
<set-propertyproperty="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> --插件方式
<set-propertyproperty="contextConfigLocation"
value="/WEB-INF/classes/applicationContext.xml"/>
</plug-in>
3)使用web.xml来使Web容器加载Spring
a、 ----在Web容器中告知Spring容器将被加载进来并进行管理
<servlet>
<servlet-name>
SpringContextServlet ----一个专门产生Spring容器的Servlet
</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup> ----Web容器一被加载,就将加载Spring容器
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
----告知applicationContext.xml的路径
</context-param>
b、----使用监听器加载Spring
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
前提:容器支持ServletAPI2.3
Spring事务管理:
1.Spring 框架中,涉及到事务管理的 API 大约有100个左右,其中最重要的有三个:
TransactionDefinition(给定的事务规则 )
PlatformTransactionManager(按照……来执行提交或者回滚操作 )
TransactionStatus(表示一个运行着的事务的状态)
2.PlatformTransactionManager的实现类:
DataSourceTransactionManager:适用于使用JDBC和iBatis进行数据持久化操作的情况。
HibernateTransactionManager:适用于使用Hibernate进行数据持久化操作的情况。
JpaTransactionManager:适用于使用JPA进行数据持久化操作的情况。
另外还有JtaTransactionManager、JdoTransactionManager、JmsTransactionManager等等
声明事务管理实例:
<beanid="transactionManager" ----产生TransactionManager
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"ref="sessionFactory"></property>
----注入SessionFactory
</bean>
<beanid="baseServiceProxy" ----引用事务管理器
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager"ref="transactionManager"/>
----注入TransactionManager
<propertyname="transactionAttributes">
<props>
<propkey="get*">PROPAGATION_REQUIRED</prop>
----将以get开头的方法加入到事务中
<propkey="execute*">PROPAGATION_REQUIRED</prop>
----将以execute开头的方法加入到事务中
</props>
</property>
</bean>
<bean id="proxyUserBO"parent="baseServiceProxy"> ----目标代理对象,继承引用事务管理器
<property name="target"ref="bo"></property> ----注入目标对象
</bean>
事务级别:
![SSH整合Spring事务管理 SSH整合Spring事务管理](https://i-blog.csdnimg.cn/blog_migrate/4507379a76380069c35138c109677ada.jpeg)