Spring注释不起作用
我将其发布到春季论坛,对 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 日志中验证查询没有以事务方式执行。
有什么想法吗?
I posted this to the spring forums, sorry for the xpost.
I am new to spring. I am working on an existing project that uses spring 1.2.8 (old, I know), and java 1.5 so annotations should work.
I am trying to use the @Transactional annotation on a concrete class, following the docs at: http://static.springsource.org/spring/docs/1.2.8/reference/transaction.html#d0e6062
So I have something like so:
<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>
and I annotate my class:
@Transactional(propagation = Propagation.REQUIRED)
public class MyDAOImpl extends JdbcDaoSupport implements MyDAO{
...
}
When I run it I can see in my debug logs that spring is finding all of the classes:
Code:
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]
but after that there is no mention of annotations or transactions. I don't even know if there are supposed to be. I am verifying in my mysql log that the queries are not being performed transactionally.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我经常忽略的一件事是,代理只能拦截从类本身外部发出的调用:如果您有一个方法调用同一类中的事务方法,则它将不会被包装通过代理。但这是对单个方法进行注释,而不是对整个类进行注释,因此这可能不是导致您的问题的原因。
One thing that I frequently overlook is that the proxy can only intercept calls if they are made from outside the class itself: If you have one method calling a transactional method in the same class, it will not be wrapped by the proxy. But that's when individual methods are annotated, not the whole class, so it's probably not what's causing your problem.
忽略 DEBUG 行(它们只是说你没有指定 id 或名称,你只有一个 bean class="")
你是否放置了该行,
你还需要在顶部添加 schemaLocation 之类的内容,
否则注释不会被处理:)
Ignore the DEBUG lines (they just say you havent specified id or name you just have a bean class="")
Have you put the line,
You also need to add the schemaLocation at the top something like
Otherwise the annotations dont get processed :)