2012-12-28 10 views
7

::ポイントカット:JavaConfig:私は春JavaConfigに、XMLコンフィギュレーションのこの作品をマッピングすることができる場合、私は思ったんだけどアドバイス

<?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を交換する方法を考え出し顧問とTX: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. @Aspectのスタイルを使用することも、既存の<aop:config>要素を変換し使用して、関連するXMLスニペットを含むことによって<aop:config>を使用して続行します。

documentationを参照すると、私はあなたがすでにほぼあなたは上記しているご使用の構成で行われていると言うでしょう。あなたはちょうどあなたがこのようにconfigコン変更する必要があります。

<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

をあなたドン場合「Tは、あなたが側面

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

そしてimporのための個別のJavaの設定クラスを作成することができ、まったくのXMLを使用したいですメインの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"); 
    } 

} 

を作成します。