2010-01-07 177 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