spring3.2.5+hibernate4.1.11声明式事务配置,无法自动提交的一个原因

web.xml---->

  <!-- 从类路径下加载spring配置文件,classpath关键字特指类路径下加载 -->
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 负责启动spring容器的监听器,它将引用上面的上下文参数获得spring配置文件地址 -->
  <listener>
  <listener-class>
  org.springframework.web.context.ContextLoaderListener
  </listener-class>
  </listener>
  
  <!-- 负责在View层打开session(lazy load) -->
  <filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- spring的核心是DispatcherServlet,它负责控制整个页面的请求路劲 -->
  <servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>
  org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  </servlet>
  <!--拦截所有以html结尾的请求-->
  <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>*.html</url-pattern>
  </servlet-mapping>
  
  <!--处理从页面传递中文到后台而出现的中文乱码问题-->
  <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>
        org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
  </filter>
  <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>*</url-pattern>
  </filter-mapping>
  
  <!-- 错误页面设置 -->
  <error-page>
  <error-code>404</error-code>
  <location>/404.htm</location>
  </error-page>

applicationContext.xml---->

<!-- 扫描com下的所有类包,应用spring的注解配置 -->
<context:component-scan base-package="com">
<context:exclude-filter type="regex" expression=".*Service"/>
</context:component-scan>
    
    <!-- 加载JDBC资源文件信息 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 获取数据源 -->
    <bean id="c3p0DataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!-- 指定连接数据库的驱动 -->
       <property name="driverClass" value="${driver}"/>
       <!-- 指定连接数据库的URL -->
       <property name="jdbcUrl" value="${url}"/>
       <!-- 指定连接数据库的用户名 -->
       <property name="user" value="${user}"/>
       <!-- 指定连接数据库的密码 -->
       <property name="password" value="${password}"/>
       <property name="maxPoolSize" value="${maxPoolSize}"/>
       <property name="minPoolSize" value="${minPoolSize}"/>
       <property name="initialPoolSize" value="${initialPoolSize}"/>
       <property name="maxIdleTime" value="${maxIdleTime}"/>
    </bean>
    
    <!-- 定义Hibernate的SessionFactory -->
    <bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="c3p0DataSource"/>
    <!-- 基于注解类的配置 -->
    <property name="packagesToScan">
    <list>
    <value>com.domain</value>
    </list>
    </property> 
    <property name="hibernateProperties">
    <!-- 设置Hibernate属性 -->
    <props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_commants">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="connection.autocommit">true</prop>
    </props>
    </property>
    </bean>
    
    <!-- 配置事务管理器 -->
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="get*" read-only="true"/>
    <tx:method name="*"/>
    </tx:attributes>
    </tx:advice>
    
    <!-- 通过AOP配置提供事务增强,让Service包下所有bean的所有方法拥有事务 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <aop:config> 
<aop:pointcut id="servicePointcut" expression="execution(* com.service..*(..))"/> 
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
</aop:config>
<bean id="employeeService" class="com.service.EmployeeService"/>
<bean id="studentService" class="com.service.StudentService"/>

java类结构

211756_Ni1W_1421734.jpg

转载于:https://my.oschina.net/u/1421734/blog/294280

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值