Java开发

Spring 中的注解

 

@Repository代表仓库. 一般注解在DAO实现类上, 别人看代码时, 就知道这个类是一个跟数据存储有关的类. 

@Service代表业务. 一般注解在Service实现类上.
@Controller代表控制器. 一般注解在控制器类上. 
如果你的类不是以上类型(数据存储类, 业务类, 控制器), 可以笼统的使用@Component

 

Spring 事物控制有声明式事物和编程式事物

声明的事物配置有XML配置和annotation 两种配置方式。

annotation 配置

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <context:annotation-config/>

    <tx:annotation-driven transaction-manager="transactionManager" /> 

      XML 配置

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

        <property name="beanNames">
            <list>
                <value>*Service</value>
                <value>*Dao</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>

     <bean id="transactionInterceptor"
        class
="org.springframework.transaction.interceptor.TransactionInterceptor" autowire="default">
        <property name="transactionManager" ref="transactionManager" />
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager"
        class
="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

      

 

posted @ 2015-02-01 15:59  King Bruce  阅读(197)  评论(0编辑  收藏  举报