English 中文(简体)
Spring Annotations not working
原标题:

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?

问题回答

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.

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





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签