我的事务xml是java config转换吗?

时间:2016-10-28 00:37:49

标签: java spring spring-java-config

以下是我们的xml与事务管理相关的部分

<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice"/>    

<aop:config proxy-target-class="true">

    <aop:pointcut id="transactionalMethods"
        expression="@annotation(org.springframework.transaction.annotation.Transactional)" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionalMethods" />

</aop:config>

我想出了这个

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement( proxyTargetClass = true )
public class TransactionConfig
{
    @Bean
    PlatformTransactionManager transactionManager( final DataSource dataSource, final EntityManagerFactory emf ) {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setDataSource( dataSource );
        jpaTransactionManager.setEntityManagerFactory( emf );
        return jpaTransactionManager;
    }

    @Bean
    PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    @Bean
    PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor() {
        return new PersistenceAnnotationBeanPostProcessor();
    }
}

它似乎工作,但阅读配置我不禁想知道我是否应该使用mode = ASPECTJ,但当我尝试这个上下文爆炸时有一个关于无法注入的异常“具体类”,因为有些东西取决于类而不是接口。

我不确定为什么我们添加了切入点,或者我们是否使用它,如果我必须将其重写为切入点,我实际上不确定我该怎么做。

我是否需要添加更多内容以获得平价?这已经是一样了吗?如果我需要添加更多我应该写的内容?

更新

尝试时

@Configuration
@EnableAspectJAutoProxy( proxyTargetClass = true )
@EnableTransactionManagement( mode = AdviceMode.ASPECTJ )

和aspectj本身通过我们的xml配置工作(方面在我们的xml配置中工作)

<context:spring-configured/>
<aop:aspectj-autoproxy/>

使用deps

[INFO] +- org.springframework.data:spring-data-jpa:jar:1.9.4.RELEASE:compile
[INFO] |  +- org.springframework:spring-orm:jar:4.2.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:4.2.7.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-expression:jar:4.2.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.2.7.RELEASE:compile
[INFO] |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.2.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:4.2.7.RELEASE:compile
[INFO] |  \- org.aspectj:aspectjrt:jar:1.8.9:compile
[INFO] +- org.springframework:spring-aspects:jar:4.2.7.RELEASE:compile
[INFO] +- org.aspectj:aspectjweaver:jar:1.8.9:compile

我收到错误,如

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.myapp.CatalogItemViewDao com.myapp.ProviderTestHistoryDao.catalogItemViewDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myapp.CatalogItemViewDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 40 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myapp.CatalogItemViewDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)

2 个答案:

答案 0 :(得分:1)

不,你不需要设置mode = ASPECTJ,除非你希望在自调用调用上应用事务,意味着从同一个类中的另一个方法调用。默认情况下,其他模式代理仅支持通过代理接口从外部进行的调用。

  

在代理模式(默认设置)下,只有外部方法调用   通过代理进入是截获的。这意味着   自调用,实际上是目标对象调用中的一个方法   目标对象的另一种方法,不会导致实际的   即使调用的方法被标记,运行时的事务也是如此   @Transactional。此外,代理必须完全初始化以提供   预期的行为,所以你不应该依赖于你的这个功能   初始化代码,

如果设置mode = aspectj,它将使用弹簧AspectJ编织,使用字节代码修改受影响的类和方法。但是这需要spring-aspect.jar和CGLIB jar存在于类路径中。

如果您只是使用@Transactional注释,则不需要定义任何aop点切割。您只需要定义事务管理器

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/transaction.html#transaction-declarative

答案 1 :(得分:0)

如果使用ASPECTJ(使用spring-aspects-4.x.y.RELEASE.jar),则切入点定义不是必需的。 看看上面提到的jar中的aop.xml,在那里定义了AnnotationTransactionAspect。