The disposition of things in spring

Source: Internet
Author: User

Spring transaction processing mechanism, to our programming has brought great convenience, one-transaction-per-request implementation mode, is I most appreciate!

In order to implement the transaction pattern when we first started contacting spring, we typically do this when declaring a transaction.

First, declare a transaction template,

<bean id= "Txproxytemplate" abstract= "true"
class= "Org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
<property name= "TransactionManager" >
<ref bean= "TransactionManager"/>
</property>
<property name= "Transactionattributes" >
<props>
<prop key= "create*" >PROPAGATION_REQUIRED</prop>
<prop key= "delete*" >PROPAGATION_REQUIRED</prop>
<prop key= "update*" >PROPAGATION_REQUIRED</prop>
<prop key= "move*" >PROPAGATION_REQUIRED</prop>
<prop key= "save*" >PROPAGATION_REQUIRED</prop>
<prop key= "*" >PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

Next, declare a service class that uses the spring transaction template

<bean id= "UserService" parent= "Txproxytemplate" >
<property name= "Target" >
<bean class= "Com.nit.training.service.impl.UserServiceImpl" >
<property name= "Userdao" ><ref bean= "Userdao"/></property>
</bean>
</property>
</bean>

***********************

This kind of declaration way I spent two years, since the contact with Lee just now, bought in his book "Spring 2.0 Treasure", after learning, found that Spring can also be configured so

The method of transaction processing based on AOP technology, first declares a transaction interceptor

<bean id= "Transactioninterceptor"
class= "Org.springframework.transaction.interceptor.TransactionInterceptor" >
<property name= "TransactionManager" ref= "TransactionManager"/>
<property name= "Transactionattributes" >
<props>
<prop key= "create*" >PROPAGATION_REQUIRED</prop>
<prop key= "update*" >PROPAGATION_REQUIRED</prop>
<prop key= "delete*" >PROPAGATION_REQUIRED</prop>
<prop key= "save*" >PROPAGATION_REQUIRED</prop>
<prop key= "list*" >PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

Next, declare an automatic proxy

<bean class= "Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
<property name= "Beannames" >
<value>*Service</value>
</property>
<property name= "Interceptornames" >
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>

<bean class= "Org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor" >
<property name= "Transactioninterceptor" ref= "Transactioninterceptor"/>
</bean>

The automatic proxy means that all service-based beans are required to use the interceptor.
The following is the configuration of the Servicebean

<bean id= "UserService" class= "Com.manbu.organization.service.impl.UserServiceImpl" autowire= "ByName" >
<property name= "Userdao" ref= "Userdao"/>
</bean>

This configuration is much simpler, although for some programmers feel that the use of copy & paste, in fact, is a similar, but, configuration files can be at least more than half, easy to maintain!

Object handling mechanism configuration issues in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.