这是我们的 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
,但是当我尝试时上下文因异常而爆炸关于无法注入(inject)“具体类”,因为有些事情取决于类而不是接口(interface)。
我不确定我们为什么要添加那个切入点,或者我们是否使用它,如果我必须将它重写为切入点,我实际上不确定我应该怎么做。
我是否需要添加更多内容才能获得奇偶校验?这已经一样了吗?如果我需要添加更多我应该写什么?
更新
尝试时
@Configuration
@EnableAspectJAutoProxy( proxyTargetClass = true )
@EnableTransactionManagement( mode = AdviceMode.ASPECTJ )
并且 aspectj 本身通过我们的 xml 配置工作(方面在我们的 xml 配置中工作)
<context:spring-configured/>
<aop:aspectj-autoproxy/>
与部门
[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)
最佳答案
不,您不需要设置 mode = ASPECTJ ,除非您希望事务应用于自调用调用,这意味着从同一类中的另一个方法调用。默认的其他模式代理将仅支持通过代理接口(interface)从外部传入的调用。
In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional. Also, the proxy must be fully initialized to provide the expected behaviour so you should not rely on this feature in your initialization code,
如果设置 mode = aspectj ,它将使用 spring AspectJ 编织,对受影响的类和方法使用字节码修改。但这将需要 spring-aspect.jar 和 CGLIB jar 出现在类路径中。
还有,
如果您只是使用@Transactional 注解,则不需要定义任何aop 切点。你只需要定义事务管理器
关于java - 我的事务 xml 到 java 配置的转换是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40296259/