1

我在xml有一個事務管理器豆如下:如何做到事務管理,而無需使用@Transactional註解

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

和會話工廠和數據源豆類:

<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close"> 
    <property name="URL" value="jdbc:oracle:thin:@localhost:1521:XE"/> 
    <property name="user" value="user"/> 
    <property name="password" value="password"/> 
</bean> 

<!-- Hibernate SessionFactory --> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 

與會議工廠擁有所有hbm文件映射。

現在我有一個UserAddressManagerImpl如下:

public class UserAddressManagerImpl implements UserAddressManager{ 
// methods to read and write in the database 
} 

和這個bean是:

<bean id="userAddressManager" class="com.sodiz.service.impl.UserAddressManagerImpl"> 

現在,這UserAddressManagerImpl沒有它@Transactional

每當我從這個類中進行任何讀取操作時,它都能正常工作,但是在進行寫入操作時會失敗。

我正在使用這個類打包在一個罐子裏。所以我不想改變這個班級。

那麼,有什麼方法可以在不使用@Transactional註釋的情況下執行讀寫操作嗎?

+0

您是否嘗試過使用xml配置? https://stackoverflow.com/questions/36917842/spring-transactional-configuring-xml –

回答

1

你將不得不使用方面來實現這一目標而無需使用註釋:

<bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

<bean id="userAddressManager" class="com.sodiz.service.impl.UserAddressManagerImpl"/> 

<tx:advice id="txAdvice" transaction-manager="txManager"> 
     <tx:attributes> 
      <tx:method name="get*" read-only="true"/> 
      <tx:method name="find*" read-only="true"/> 
      <tx:method name="*"/> 
     </tx:attributes> 
</tx:advice> 

<aop:config> 
    <aop:pointcut id="userAddressManagerOperation" 
     expression="execution(* com.sodiz.service.impl.UserAddressManagerImpl.*(..))"/> 
    <aop:advisor advice-ref="txAdvice" pointcut-ref="userAddressManagerOperation"/> 
</aop:config> 

當然,這樣在你的包每個服務適用於上述事務設置,你可以使用更多的wilcards。

+0

我試過這個解決方案後,添加了POM依賴。現在它給了我一個java.lang.VerifyError的錯誤。代碼結束的下降。 – Tarun

+0

確保您在beans標記中定義了以下內容:xmlns:aop =「http://www.springframework.org/schema/aop」 xmlns:tx =「http://www.springframework.org/schema/tx 「 –

+0

我在我的beans標記中已經有xmlns:aop =」http://www.springframework.org/schema/aop「 和xmlns:tx =」http://www.springframework.org/schema/tx「 – Tarun

相關問題