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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.0.xsd"
    default-lazy-init="true">

    <description>spring配置</description>
    <!-- mvc注解模式 -->
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

    <context:component-scan base-package="sxq.mvn.demo" />
    
    <!-- 内部资源视图解析器 ,就是加  前缀,后缀的-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/content/" />
        <property name="suffix" value=".jsp"></property>
    </bean>
    

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url" value="jdbc:sqlserver://192.168.9.99:1433;databaseName=energy" />
        <property name="username" value="sa" />
        <property name="password" value="123qwe!@#QWE" />
        <!-- 
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/hibernate_test" />
        <property name="username" value="root" />
        <property name="password" value="sxq" />
         -->
    </bean>

    <!-- 将SessionFactory交给Spring来管理 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="namingStrategy">
            <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
        </property>
        <property name="hibernateProperties">
            <props>
                <!-- 
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <!-- 
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
                 -->
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>sxq.mvn.demo.entity</value>
            </list>
        </property>
    </bean>

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

    <!-- 配置哪些地方需要进行事务管理 -->
    <aop:config>
        <aop:pointcut id="allServiceMethods"
            expression="execution(* sxq.mvn.demo.manager.*.*(..))" />
        <aop:advisor advice-ref="txAdvice"
            pointcut-ref="allServiceMethods" />
    </aop:config>

    <!-- 配置事务特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 所有save开头的方法,都使用事务 -->
            <tx:method name="save*" />
            <!-- 所有del开头的方法,都使用事务 -->
            <tx:method name="del*" />
            <tx:method name="add*" />
            <tx:method name="update*" />

            <!-- 所有其它方法,都使用事务,而且是只读的,即不允许在这些方法中做CUD操作!
                FlushMode -  AUTO/MANUAL
            -->
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
</beans>

applicationContext.xml,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。