2012-12-28 66 views
7

我想知道如果我可以映射這塊XML的配置,以春季JavaConfig:JavaConfig:更換AOP:顧問和TX:建議

<?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:切入點

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

任何提示如何更換休息?

回答

3

目前,它不可能把所有基於XML的AspectJ設置一個基於Java的配置。可能它永遠不會。主要原因是Java不支持方法文字。但是有一個解決辦法,這是首次這裏介紹:https://jira.springsource.org/browse/SPR-8148

  1. 繼續通過包括使用@ImportResource
  2. 將任何現有<aop:config>元素使用@Aspect風格相關的XML片斷使用<aop:config>

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

我希望我能幫助...

+0

謝謝(尤其是指出傑拉問題)! – user871611

5

如果你不「不想使用任何XML的話,那麼你可以創建方面

@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 
} 

而且祁門功夫一個單獨的Java配置類T上方配置類在主的AppConfig類

@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"); 
    } 

} 

您可以諮詢註釋一起使用切入點,如上圖所示。