SSH整合配置

1.spring和struts的整合
  加入struts2-spring-plugin-2.3.24.jar插件包即可,配置文件不需要整合,整合过程比较简单。

  web.xml

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

   ContextLoaderListener实现ServletContextListener接口,在服务器启动时加载,
   默认自动载入置于WEB-INF目录下的Spring配置文件applicationContext.xml

2.spring和hibernate的整合

  参考文档

  http://www.open-open.com/lib/view/open1414743802278.html
  http://blog.youkuaiyun.com/qq7342272/article/details/7928814

 (1)引入申明式事务支持的XML命名空间

  xmlns:tx="http://www.springframework.org/schema/tx"

  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

  (2)dataSource配置
   
    占位符
    
    常用的连接池

    (i)apache的数据源 org.apache.commons.dbcp.BasicDataSource           
    (ii)阿里巴巴的数据源 com.alibaba.druid.pool.DruidDataSource
    (iii)c3p0 com.mchange.v2.c3p0.ComboPooledDataSource
    (iv)Proxool

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- 初始化连接数量; -->
        <property name="initialSize" value="0" />
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="20" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="20" />
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="0" />
        <!-- 最大等待时长 -->
        <property name="maxWait" value="60000" />
        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超过时间限制多长; -->
        <property name="removeAbandonedTimeout" value="180"/>     
          
          
        <!-- 数据源连接参数配置; -->
        <property name="username" value="${db.username}"/>
        <property name="url" value="${db.url}"/>
        <property name="password" value="${db.password}"/>
        <property name="driverClassName" value="${db.driverClassName}"/>
          
    </bean> 
   
   (3)读取外部属性文件,获取数据源参数

    <context:property-placeholder location="classpath:dataSource.properties/>

   (4)sessionFactory配置
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>    
        <property name="packagesToScan" value="net.xinqushi.model.model"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
    </bean>

  (5)配置事务管理器
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean> 

 (6)初始化hibernateTemplate
  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
   <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>

  (7)配置事务管理
        
    <!-- 定义切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* net.xinqushi.dao.impl.*.* (..))" id="txPointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
      
    <!-- 声明式事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">            
        <tx:attributes>
          <tx:method name="add*" propagation="REQUIRED"/>
          <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
        </tx:attributes>        
    </tx:advice>

  
   之前的aop配置
   
    <aop:config>
        <aop:pointcut expression="execution" id=""/>
        <aop:aspect ref="log">
            <aop:before method="mybefore"/>
        </aop:aspect>
    </aop:config>

 

完整代码

<?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:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/tx
  		http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		">       
	<context:annotation-config/>	
	<context:component-scan base-package="net.xinqushi.dao.impl,net.xinqushi.aop"/> 
	 <!-- 实现AOP:注解配置方式 --> 
	<aop:aspectj-autoproxy/>
	
    <!-- 实现AOP:XML配置方式 -->   
   <aop:config>
   <aop:pointcut 
            expression="execution(* net.xinqushi.dao.impl.*.*(..))" id="pt"/>
   		<aop:aspect ref="log">    		
			<aop:before method="myBefore" pointcut-ref="pt"/>
			<aop:after method="myEnd" pointcut-ref="pt"/>
   		</aop:aspect>
	</aop:config>
	<!-- 读取外部属性文件,获取数据源参数 -->
	<context:property-placeholder location="classpath:dataSource.properties"/>  
	<!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- 初始化连接数量; -->
        <property name="initialSize" value="0" />
        <!-- 最大并发连接数 -->
        <property name="maxActive" value="20" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="20" />
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="0" />
        <!-- 最大等待时长 -->
        <property name="maxWait" value="60000" />
        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超过时间限制多长; -->
        <property name="removeAbandonedTimeout" value="180"/>     
                 
        <!-- 数据源连接参数配置; -->
        <property name="username" value="${db.username}"/>
        <property name="url" value="${db.url}"/>
        <property name="password" value="${db.password}"/>
        <property name="driverClassName" value="${db.driverClassName}"/>       
    </bean> 
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>    
        <property name="packagesToScan" value="net.xinqushi.model"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
            </props>
        </property>
   	</bean>
   	
   	  <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean> 
    <!-- 初始化hibernateTemplate -->
     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
   <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
   
    <!-- 定义切面 -->
    <aop:config>
        <aop:pointcut expression="execution(* net.xinqushi.dao.impl.*.* (..))" id="txPointCut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
      
    <!-- 声明式事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">            
        <tx:attributes>
	      <tx:method name="add*" propagation="REQUIRED"/>
	      <tx:method name="get*" read-only="false" propagation="REQUIRED"/>
        </tx:attributes>        
    </tx:advice>
</beans>

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值