您的位置:首页 > 编程语言 > Java开发

struts+hibernate+spring整合

2008-12-12 21:55 357 查看
在web.xml中

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/spring-config/spring-dao.xml</param-value>

</context-param>

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

在struts-config.xml中用spring进行请求转发

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor">

</controller>

并把spring IOC配置文件spring-bean.xml在启动时加载

<plug-in

className="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation"

value="/WEB-INF/spring-config/spring-bean.xml" />

</plug-in>

在spring-dao.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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 数据源 -->

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName">

<value>java:comp/env/jdbc/logis</value>

</property>

</bean>

<!-- sessionFactory -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">

<ref local="dataSource"/>

</property>

<property name="mappingResources">

<list>

<value>cn/com/csu/vo/UserVo.hbm.xml</value>

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.OracleDialect

</prop>

<prop key="hibernate.show_sql">true</prop>

</props>

</property>

</bean>

<!-- 模板 -->

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

<constructor-arg>

<ref local="sessionFactory"/>

</constructor-arg>

</bean>

<!-- 事务管理 -->

<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory">

<ref local="sessionFactory"/>

</property>

</bean>

<!-- 代理设置 -->

<bean id="daoProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

<property name="transactionManager">

<ref local="hibernateTransactionManager"/>

</property>

<property name="transactionAttributes">

<props>

<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>

</props>

</property>

</bean>

<!-- DAO配置 -->

<bean id="LoginDaoImpl" parent="daoProxy">

<property name="target">

<bean class="cn.com.csu.dao.impl.LoginDaoImpl">

<property name="hibernateTemplate">

<ref local="hibernateTemplate"/>

</property>

</bean>

</property>

</bean>

</beans>

在spring-bean.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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-lazy-init="true"

>

<bean id="LoginServiceImpl" class="cn.com.csu.service.impl.LoginServiceImpl">

<property name="loginDaoImpl">

<ref bean="LoginDaoImpl"/>

</property>

</bean>

<bean name="/login" class="cn.com.csu.action.LoginAction">

<property name="loginServiceImpl">

<ref local="LoginServiceImpl"/>

</property>

</bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: