SSH框架整合之Spring配置(applicationContext)

来源:互联网 发布:传感器 java 编辑:程序博客网 时间:2024/06/06 17:08
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        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.2.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
    
    <!-- 读取db.properties文件 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 配置c3p0连接池 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
        <property name="driverClass" value="${jdbc.driverClass}" ></property>
        <property name="user" value="${jdbc.user}" ></property>
        <property name="password" value="${jdbc.password}" ></property>
    </bean>
    
    <!-- 核心事务管理器 -->
    <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager" >
        <property name="sessionFactory" ref="sessionFactory" ></property>
    </bean>
    
    <!-- 配置通知 -->
    <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager" >
        <tx:attributes>
            <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice> -->
    <!-- 配置将通知织入目标对象
    配置切点
    配置切面 -->
    <!-- <aop:config>
        <aop:pointcut expression="execution(* cn.itcast.service.impl.*ServiceImpl.*(..))" id="txPc"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
    </aop:config> -->
    <!-- ========================================================================================= -->
    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    
    <!-- 将SessionFactory配置到spring容器中 -->
    <!-- 加载配置方案1:仍然使用外部的hibernate.cfg.xml配置信息 -->
    <!-- <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
        <property name="configLocation" value="classpath:hibernate.cfg.xml" ></property>
    </bean> -->
    <!-- 加载配置方案2:在spring配置中放置hibernate配置信息 -->
    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" >
        <!-- 将连接池注入到sessionFactory, hibernate会通过连接池获得连接 -->
        <property name="dataSource" ref="dataSource" ></property>
        <!-- 配置hibernate基本信息 -->
        <property name="hibernateProperties">
            <props>
                <!--  必选配置 -->
            <!--     <prop key="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</prop>
                <prop key="hibernate.connection.url" >jdbc:mysql:///crm_32</prop>
                <prop key="hibernate.connection.username" >root</prop>
                <prop key="hibernate.connection.password" >1234</prop> -->
                <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
                
                <!--  可选配置 -->
                <prop key="hibernate.show_sql" >true</prop>
                <prop key="hibernate.format_sql" >true</prop>
                <prop key="hibernate.hbm2ddl.auto" >update</prop>
            </props>
        </property>
        <!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
        <property name="mappingDirectoryLocations" value="classpath:cn/itcast/domain" ></property>
    </bean>
    
    <!-- action -->
    <!-- 注意:Action对象作用范围一定是多例的.这样才符合struts2架构 -->
    <bean name="userAction" class="cn.itcast.web.action.UserAction" scope="prototype" >
        <property name="userService" ref="userService" ></property>
    </bean>
    <!-- service -->
    <bean name="userService" class="cn.itcast.service.impl.UserServiceImpl" >
        <property name="ud" ref="userDao" ></property>
    </bean>
    <!-- dao -->
    <bean name="userDao" class="cn.itcast.dao.impl.UserDaoImpl" >
        <!-- 注入sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory" ></property>
    </bean>
</beans>
原创粉丝点击