JavaConfig:替换aop:advisor和tx:advice [英] JavaConfig: Replacing aop:advisor and tx:advice

查看:150
本文介绍了JavaConfig:替换aop:advisor和tx:advice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将此xml配置映射到Spring JavaConfig:

I'm wondering if I can map this piece of xml-configuration to Spring JavaConfig:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
                      http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop.xsd
                      http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx.xsd"
  default-autowire="byName">

  <aop:config>
     <aop:pointcut id="serviceAnnotatedClass" expression="@within(org.springframework.stereotype.Service)" />
     <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut-ref="serviceAnnotatedClass" order="20" />
  </aop:config>

  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="get*" read-only="true" />
      <tx:method name="find*" read-only="true" />
      <tx:method name="load*" read-only="true" />
      <tx:method name="is*" read-only="true" />
      <tx:method name="ownTransaction*" propagation="REQUIRES_NEW" rollback-for="Exception" />
      <tx:method name="*" rollback-for="Exception" />
    </tx:attributes>
  </tx:advice>

</beans>

到目前为止,我已经弄清楚如何用

So far I figured out how to replace aop:pointcut with

<aop:advisor id="managerTx" advice-ref="txAdvice" 
pointcut="com.myapp.configuration.AspectConfig.serviceAnnotatedClass()" order="20"/>

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AspectConfig
{

  @Pointcut("@within(org.springframework.stereotype.Service)")
  public void serviceAnnotatedClass() {}
}

有人提示如何更换其余的吗?

Any hints how to replace the rest?

推荐答案

当前无法将所有基于XML的AspectJ设置转换为基于Java的配置.可能永远不会.主要原因是Java不支持方法文字.但是有一种解决方法,它首先在这里提出: https://jira.springsource.org/browse/SPR-8148

Currently it isn't possible to translate all XML-based AspectJ settings to a Java based configuration. Probably it will never be. The main reason is that Java doesn't support method literals. But there is a workaround, which was first presented here: https://jira.springsource.org/browse/SPR-8148

  1. 通过使用@ImportResource
  2. 包含相关的XML代码段来继续使用<aop:config>
  3. 将任何现有的<aop:config>元素转换为使用@Aspect样式.
  1. Continue using <aop:config> by including the relevant XML snippet using @ImportResource
  2. Convert any existing <aop:config> elements to use @Aspect style.

请参阅文档,我想说您已经完成了上述配置.您只需要像这样更改您的配置即可:

Referring to the documentation, I would say that you're already nearly done with your configuration you have described above. You just have to change you config like this:

<aop:config>
     <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="com.myapp.configuration.AspectConfig.serviceAnnotatedClass()" order="20" />
</aop:config>

其余部分保持原样并导入该资源:

Leave the rest like it is and import that resource:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
@ImportResource("classpath:/aop-config.xml")
public class AspectConfig
{
    @Pointcut("@within(org.springframework.stereotype.Service)")
    public void serviceAnnotatedClass() {}
}

我希望我能帮忙...

I hope I could help...

这篇关于JavaConfig:替换aop:advisor和tx:advice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆