<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <link rel="stylesheet" href="css/stdlayout.css" type="text/css"> <link rel="stylesheet" href="css/print.css" type="text/css"> <meta content="text/html; charset=Big5" http-equiv="content-type"> <title>TransactionAttributeSource、 TransactionAttribute</title></head><body><h3><a href="http://caterpillar.onlyfun.net/Gossip/index.html">FromGossip@caterpillar</a></h3><h1><a href="SpringGossip.html">Spring Gossip: TransactionAttributeSource、 TransactionAttribute</a></h1><table style="text-align: left; width: 946px; height: 32px;" border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="vertical-align: top; width: 250px; text-align: center;"> <!--google_ad_client = "";google_ad_width = 160;google_ad_height = 600;google_ad_format = "160x600_as";google_ad_type = "text_image";google_ad_channel = "";//--> <br> <br> <!--google_ad_client = "";google_ad_width = 160;google_ad_height = 600;google_ad_format = "160x600_as";google_ad_type = "text_image";google_ad_channel = "";//--> </td> <td style="vertical-align: top; width: 690px; text-align: left;"> <small>在TransactionProxyFactoryBean上有setTransactionAttributeSource()與setTransactionAttributes()方法,它們是用來設定交易屬性的策略實例。<br> <br>org.springframework.transaction.interceptor.TransactionAttributeSource介面上有一個getTransactionAttribute()方法,您可以根據傳遞給它的Method實例與Class實例,決定該返回一個什麼內容的org.springframework.transaction.interceptor.TransactionAttribute實例,一個最簡單的TransactionAttributeSource實作是org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource,對於每一個方法呼叫都會應用交易,它會返回的TransactionAttribute實例之預設傳播行為是PROPAGATION_REQUIRED,隔離層級為ISOLATION_DEFAULE。<br> <br>一個應用的例子如下所示:<br> </small> <div style="margin-left: 40px;"><small style="font-weight: bold; font-family: Courier New,Courier,monospace;">...</small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"><bean id="transactionAttributeSource"</small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> class="org.springframework.transaction.interceptor.</small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> → MatchAlwaysTransactionAttributeSource"/></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"><bean id="userDAOProxy"</small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> class="org.springframework.transaction.</small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> → interceptor.TransactionProxyFactoryBean"></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <property name="proxyInterfaces"></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <list></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <value>onlyfun.caterpillar.IUserDAO</value></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> </list></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> </property></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <property name="target"></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <ref bean="userDAO"/></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> </property></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <property name="transactionManager"></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <ref bean="transactionManager"/></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> </property></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <property name="transactionAttributeSource"></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <ref bean="transactionAttributeSource"/></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"> </property></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-weight: bold; font-family: Courier New,Courier,monospace;"></bean></small><br> </div> <small>...<br> <br> <br>您可以使用org.springframework.transaction.interceptor.DefaultTransactionAttribute,並設置自己的交易策略,之後將之設定給TransactionAttributeSource,例如:<br> </small> <div style="margin-left: 40px;"><small style="font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">...</span></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;"><bean id="myTransactionAttribute"</span></small><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <small style="font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;"> class="org.springframework.transaction.</span></small><br style="font-weight: bold; font-family: Courier Ne
评论1
最新资源