【问题标题】:JavaConfig: Replacing aop:advisor and tx:adviceJavaConfig:替换 aop:advisor 和 tx:advice
【发布时间】:2016-12-29 22:35:57
【问题描述】:

我想知道是否可以将这段 xml 配置映射到 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>

到目前为止,我想出了如何用

替换 aop:pointcut
<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() {}
}

任何提示如何替换其余部分?

【问题讨论】:

    标签: spring


    【解决方案1】:

    如果你根本不想使用任何xml,那么你可以为方面创建一个单独的Java配置类

    @Configuration
    @EnableAspectJAutoProxy
    @ComponentScan(basePackages = "com.myAspects")
    public class AspectConfig {
        //Here you can define Aspect beans or just use @ComponentScan (as above)
        //to scan the @Aspect annotation in com.myAspects package
    }
    

    并在您的主 AppConfig 类中导入上述配置类

    @Configuration
    @EnableWebMvc
    @Import({ AspectConfig.class })
    @ComponentScan(basePackages = { "pkg1", "pkg2", "pkg3" })
    public class AppConfiguration extends WebMvcConfigurationSupport {
        //Other configuration beans or methods
    }
    

    现在创建你的方面 bean

    import com.myAspects;
    @Component
    @Aspect
    public class LoggingAspect {
    
        @Before("execution(* com.service.*.*(..))")
        public void logBefore(){
            System.out.println("before advice called");
        } 
    
        @After("execution(* com.service.*.*(..))")
        public void logAfter(){
            System.out.println("after advice called");
        } 
    
    }
    

    您可以使用切入点和如上所示的通知注释。

    【讨论】:

      【解决方案2】:

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

      1. 通过使用@ImportResource 包含相关的XML sn-p 继续使用&lt;aop:config&gt;
      2. 将任何现有的&lt;aop:config&gt; 元素转换为使用@Aspect 样式。

      参考documentation,我想说您已经完成了上面描述的配置。你只需要像这样改变你的配置:

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

      保留其余部分并导入该资源:

      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() {}
      }
      

      希望能帮到你……

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-03
        • 1970-01-01
        相关资源
        最近更新 更多