Spring S2SH集成开发

[size=small][color=olive][b]<!--
S2SH 集成开发配置 Struts2.0 + Spring 3 + Hibernate 3 开发框架
大纲:
1、配置Struts2.0 环境
2、配置Spring 环境
3、配置Hibernate 环境


提示:相关jar包

知识点:使用自动生成的DAO层,存在数据安全性问题,所以必须引入事务管理;AOP[/b][/color][/size]

[size=medium][color=red][b]A、web.xml 配置[/b][/color][/size]

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Struts2.0 的环境 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置spring监听 -->
<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>
</web-app>


[size=medium][color=red][b]B、struts.xml 配置[/b][/color][/size]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
<!-- 说明struts 与 spring 的关系 -->
<constant name="struts.objectFactory" value="spring"></constant>
<include file="user.xml"></include>
</struts>



[size=medium][color=red][b]C、application.xml 配置[/b][/color][/size]

<?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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

<!-- 配置数据源beans -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL">
</property>
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
</bean>

<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>

<!-- 方言,认证语言 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>

<!-- 配置映射关系,加入持久化对象的配置文件 -->
<property name="mappingResources">
<list>
<value>com/svse/entity/TUser.hbm.xml</value>
</list>
</property>
</bean>

<!--
事务:AOP , 说明:由于自动生成的DAO 存在数据安全性的问题,所以必须在Spring 中 进行事务的结合,确保 数据的安全性;
org.springframework.orm.hibernate3.HibernateTransactionManager :
Hibenate事务管理
-->

<!-- 声明一个事务管理者 -->
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 接收通知,使用事务 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务管理器 -->
<tx:advice id="userTransaction" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 注入AOP切面,说明事务使用的时刻 -->
<aop:config>
<aop:pointcut expression="execution(* com.svse.impl.*.*(..))"
id="allMethods" />
<!-- 通过aop:advisor 引用具体的事务,以及事务使用的时刻 -->
<aop:advisor advice-ref="userTransaction" pointcut-ref="allMethods" />
</aop:config>


<!-- 注入DAO , 并指定引用关系 -->
<bean id="TUserDAO" class="com.svse.dao.TUserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<!-- 注入业务逻辑 -->
<bean id="userImpl" class="com.svse.impl.UserImpl">
<property name="userdao" ref="TUserDAO"></property>
</bean>

<!-- 注入Action ,声明关系,并指明为代理模式-->
<bean id="userAction" class="com.svse.action.UserAction" scope="prototype">
<property name="userService" ref="userImpl"></property>
</bean>


</beans>

-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值