के लिए एक AspectJ सलाह मैं आदिम Pointcut के साथ सरल पहलू लिखा और विधि सलाह है:सेट स्थिर विधि
@Aspect
public class MyAspect {
@Pointcut("execution(static * com.mtag.util.SomeUtil.someMethod(..))")
public void someMethodInvoke() { }
@AfterReturning(value = "someMethodInvoke())", returning = "comparisonResult")
public void decrementProductCount(List<String> comparisonResult) {
//some actions
}
}
मैं निम्नलिखित है वसंत एनोटेशन आधारित अनुप्रयोग config:
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {
//...
}
और उपयोगिता में वर्ग com.mtag.util पैकेज:
public class SomeUtil {
static List<String> someMethod(List<String> oldList, List<String> newList) {
//...
}
}
लेकिन
जब मैं फोन +०१२३५१६४१०SomeUtil.someMethod(arg1, arg2);
यूनिट परीक्षण में मैं देख सकता हूं कि विधि कॉल को अवरुद्ध नहीं किया गया है और मेरी @AfterReturning सलाह काम नहीं कर रही है।
लेकिन अगर मैं
@Pointcut("execution(* com.mtag.util.SomeUtil.someMethod(..))")
करने के लिए बदल (गैर स्थिर) विधि, pointcut someMethod() उदाहरण के लिए टाइप करें और @Component एनोटेशन और कॉल लक्ष्य जोड़कर स्प्रिंग द्वारा SomeUtil सेम का प्रबंधन इस तरह metod:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class}, loader = AnnotationConfigContextLoader.class)
public class SomeUtilTest {
@Autowired
private SomeUtil someUtil;
@Test
public void categoriesDiffCalc() {
List<String> result = someUtil.someMethod(...);
}
}
सबकुछ ठीक है।
किस तरह से मैं स्थिर विधि के लिए एक सलाह सेट कर सकता हूं?
'@AfterReturning (value =" someMethodInvoke()) "' आप एक अतिरिक्त ब्रैकेट – tibtof
@izhamoidsin है, बशर्ते जवाब है पढ़ें। या यह गलत है? –