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{
...
}

当我运行它时,我可以在调试日志中看到 spring 正在查找所有类: 代码:

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 日志中验证查询没有以事务方式执行。

有任何想法吗?

4

2 回答 2

2

我经常忽略的一件事是,代理只能拦截来自类本身外部的调用:如果您有一个方法调用同一类中的事务方法,它将不会被代理包装。但那是注释单个方法的时候,而不是整个类,所以它可能不是导致你的问题的原因。

于 2010-01-10T12:50:07.813 回答
1

Ignore the DEBUG lines (they just say you havent specified id or name you just have a bean class="")

Have you put the line,

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

You also need to add the schemaLocation at the top something like

<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>

Otherwise the annotations dont get processed :)

于 2010-01-12T16:51:23.940 回答