2010-01-07 175 views
2

我發佈了這個春季論壇,抱歉爲xpost。春季註釋不起作用

我是新來的春天。我正在使用Spring 1.2.8(老,我知道)和Java 1.5的現有項目,所以註釋應該工作。

我試圖使用@Transactional註釋上一個具體的類,下面的文檔在:http://static.springsource.org/spring/docs/1.2.8/reference/transaction.html#d0e6062

所以我有一些像這樣:

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
<property name="dataSource" ref="DataSource"/> 
</bean> 

<bean id="MyDAO" 
    class="com.company.package.dao.spring.MyDAOImpl"> 
    <property name="dataSource" ref="DataSource" /> 
</bean> 

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

<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"> 
    <property name="transactionInterceptor" ref="txInterceptor"/> 
</bean> 

<bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
    <property name="transactionManager" ref="transactionManager"/> 
    <property name="transactionAttributeSource"> 
    <bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/> 
    </property> 
</bean> 

,我批註我的課:

@Transactional(propagation = Propagation.REQUIRED) 
public class MyDAOImpl extends JdbcDaoSupport implements MyDAO{ 
... 
} 

當我運行它時,我可以在我的調試日誌中看到春天正在查找所有類: 代碼:

01-07-10 12:10:45 DEBUG [DefaultXmlBeanDefinitionParser] Neither XML 'id' nor 'name' specified - using generated bean name [org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator] 
01-07-10 12:10:45 DEBUG [DefaultXmlBeanDefinitionParser] Neither XML 'id' nor 'name' specified - using generated bean name [org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor] 
01-07-10 12:10:45 DEBUG [DefaultXmlBeanDefinitionParser] Neither XML 'id' nor 'name' specified - using generated bean name [org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#329f3d] 

但之後沒有提及註釋或交易。我甚至不知道是否應該有。我在我的mysql日誌中驗證這些查詢沒有被執行。

任何想法?

+0

您是否嘗試使用編程式事務管理(TransactionTemplate)?它是否產生了預期的交易行爲? – axtavt 2010-01-07 20:05:58

+0

我還沒有用TransactionTemplate嘗試過它,但是我已經聲明地做了它沒有註釋和使用org.springframework.transaction.interceptor.TransactionProxyFactoryBean類。這讓我可以事務訪問數據庫,最終我想要事務訪問和註釋。 – DanInDC 2010-01-07 20:53:23

+0

而你如何從MyDAOImpl調用方法? – axtavt 2010-01-07 21:07:26

回答

2

有一兩件事,我經常忽視的是,代理只能攔截如果從的調用類本身:如果你有一個方法調用同一個類中的事務性方法,它不會被包裹代理人。但那是個別方法被註釋的時候,而不是整個類,所以這可能不是什麼導致你的問題。

1

忽略DEBUG線(他們只是說你還沒有指定的ID或名字你只是有一個bean類=「」)

你有沒有放線,

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

您還需要添加頂部類似

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> 
</beans> 

否則,註釋不中的schemaLocation得到處理:)

+0

嘿,謝謝你的回覆。違揹我的意願,我正在使用1.2.8版本,但事實就是如此。我相信tx:註解驅動在Spring 2.0之前不會推出。另外,我爲1.2.8鏈接的文檔沒有提及它... – DanInDC 2010-01-13 14:27:11