2012-12-28 15 views
7

अगर मैं वसंत 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 को बदलने के लिए: 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() {} 
} 

कोई संकेत को बदलने के लिए कैसे के साथ आराम?

उत्तर

3

वर्तमान में सभी XML- आधारित AspectJ सेटिंग्स को जावा आधारित कॉन्फ़िगरेशन में अनुवाद करना संभव नहीं है। शायद यह कभी नहीं होगा। मुख्य कारण यह है कि जावा विधि अक्षर का समर्थन नहीं करता है। लेकिन वहाँ एक समाधान है, जो पहली यहां पेश किया गया है: https://jira.springsource.org/browse/SPR-8148

  1. का उपयोग कर @ImportResource
  2. @Aspect शैली का उपयोग करने से किसी भी मौजूदा <aop:config> तत्वों कन्वर्ट प्रासंगिक एक्सएमएल स्निपेट शामिल द्वारा <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

यदि आपके पास 'टी किसी भी एक्सएमएल उपयोग करना चाहते हैं, तो आप पहलुओं

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

अब आप अपने पहलू सेम

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

} 

बनाते हैं तो आप सलाह एनोटेशन के साथ pointcut उपयोग कर सकते हैं जैसा कि ऊपर दिखाया।

संबंधित मुद्दे