SSH框架整合--applicationContext.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"
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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
" >
<!-- 引入log4j.properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:component-scan base-package="com.ssh.dao,com.ssh.service"/>

<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 数据源个性化配置,根据实际项目需求进行: -->
<property name="initialPoolSize" value="3"/>
<property name="minPoolSize" value="3"/>
<property name="maxPoolSize" value="15"/>
<property name="maxConnectionAge" value="28800"/>
<!-- 最大闲置时间 单位秒 设置为6小时,主要目的避免mysql8小时陷阱-->
<property name="maxIdleTime" value="21600"/>
</bean>

<!-- 配置hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 加载c3p0配置 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定Hibernate配置信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<!-- 使用声明式事务管理时,配置该信息将抛出org.hibernate.HibernateException: save is not valid without active transaction(原因未知) -->
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<!-- 指定要扫描的实体类所在的包 -->
<property name="packagesToScan" value="com.ssh.entity"></property>
</bean>

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

<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- 为不同目标方法指定不同事务属性 -->
<tx:attributes>
<!-- name指定该事务属性针对的目标方法,propagation指定事务传播特性,read-only指定为是否为只读事务 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="merge*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="put*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="count*" propagation="SUPPORTS" read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="list*" propagation="SUPPORTS" read-only="true" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 定义事务处理的AOP切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ssh.service.impl..*.*(..))"/>
</aop:config>

<!-- 将Struts2的action交个Spring来管理 -->
<bean id="testAction" class="com.ssh.action.TestAction"></bean>

</beans>

原文地址:https://www.cnblogs.com/yueguanguanyun/p/7374409.html