Spring JavaConfig:替换aop:advisor和tx:advice

Spring JavaConfig:替换aop:advisor和tx:advice,spring,Spring,我想知道是否可以将这段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/sche

我想知道是否可以将这段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>

有没有关于如何替换其余部分的提示?

目前无法将所有基于XML的AspectJ设置转换为基于Java的配置。也许永远不会。主要原因是Java不支持方法文本。但这里首先介绍了一种变通方法:

  • 继续使用
    ,我想说您已经基本完成了上面描述的配置。您只需按如下方式更改配置:

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

    我希望我能帮助您……

    如果您根本不想使用任何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");
        } 
    
    }
    
    您可以将切入点与通知注释一起使用,如上所示

    @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
    }
    
    @Configuration
    @EnableWebMvc
    @Import({ AspectConfig.class })
    @ComponentScan(basePackages = { "pkg1", "pkg2", "pkg3" })
    public class AppConfiguration extends WebMvcConfigurationSupport {
        //Other configuration beans or methods
    }
    
    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");
        } 
    
    }