首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

spring2.5.6施用总结

2012-06-28 
spring2.5.6使用总结最少所需jar包jdbc.propertiesjdbc.driverClassNamecom.mysql.jdbc.Driverjdbc.urlj

spring2.5.6使用总结
最少所需jar包

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=123

beans.xml(applicationContest.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-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- 开启注解方式使用IOC -->
    <context:annotation-config />
    <context:component-scan base-package="com.anllin" />
   
    <!-- 开启注解方式使用AOP -->
    <!--<aop:aspectj-autoproxy/>-->

    <!-- 自动读取jdbc.properties里的配置 -->
    <!--<context:property-placeholder location="classpath:jdbc.properties"/>-->
   
    <bean
        value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring" />
        <property name="username" value="root" />
        <property name="password" value="123" />
    </bean>
    -->

    <bean id="dataSource" destroy-method="close"
        value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactory"
        ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <value>com.anllin.registration.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <!-- spring事务的annotation配置 ,需要多个配置时会比较繁琐,少量配置时会方便很多-->
    <!--    <tx:annotation-driven transaction-manager="txManager"/>-->

    <!-- 开启事务管理 -->
    <bean id="txManager"
        ref="sessionFactory" />
    </bean>

    <!--xml方式配置切面 -->
    <!--
    <bean id="userService" ref="logInterceptor">
            <aop:pointcut expression="execution(* com.anllin.registration.service..*.*(..))"
                id="servicePointCut" />
            <aop:before method="before" pointcut-ref="servicePointCut" />
        </aop:aspect>
    </aop:config>
     -->   

    <!-- spring事务的xml配置 ,建议使用-->
    <aop:config>
        <aop:pointcut id="bussinessService"
            expression="execution(* com.anllin.registration.service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" />
    </aop:config>

    <!-- 对不同的方法进行不同的事务管理 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" propagation="NEVER" />
            <tx:method name="isExists*" read-only="true" propagation="NEVER" />
            <tx:method name="*" propagation="REQUIRED" read-only="false" />
        </tx:attributes>
    </tx:advice>

    <!-- 实现hibernateTemplate注入 -->
    <bean id="hibernateTemplate" ref="sessionFactory" />
    </bean>

    <!--实现HibernateDaoSupport注入 -->
    <!--
    <bean id="abstractDao" ref="sessionFactory"></property>
    </bean>
    -->
</beans>

热点排行