I use Spring 3.2.0.
1. <context:load-time-weaver> and AspectJ is worked
-- XML configuration
-- META-INF/aop.xmlCode:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:load-time-weaver /> <bean id="readArticleService" class="sp32.board.service.ReadArticleServiceImpl" /> ...
-- ProfilingAspect classCode:<aspectj> <weaver> <include within="sp32.board.service.*" /> <include within="sp32.aop.annot.*" /> </weaver> <aspects> <aspect name="sp32.aop.annot.ProfilingAspect" /> </aspects> </aspectj>
-- Main classCode:@Aspect public class ProfilingAspect { @Pointcut("execution(public * sp32.board..*(..))") private void profileTarget() {} @Around("profileTarget()") public Object trace(ProceedingJoinPoint joinPoint) throws Throwable { String signatureString = joinPoint.getSignature().toShortString(); System.out.println(signatureString + " started"); long start = System.currentTimeMillis(); try { Object result = joinPoint.proceed(); return result; } finally { long finish = System.currentTimeMillis(); System.out.println(signatureString + " finished"); System.out.println(signatureString + " running time : " + (finish - start) + "ms"); } } }
-- runCode:public class MainLTW { public static void main(String[] args) throws ArticleNotFoundException { String[] configLocations = new String[] { "classpath:/acLTW.xml" }; ApplicationContext context = new GenericXmlApplicationContext( configLocations); ReadArticleService readArticleService = new ReadArticleServiceImpl(); readArticleService.getArticleAndIncreaseReadCount(10); WriteArticleService writeArticleService = context.getBean( "writeArticleService", WriteArticleService.class); writeArticleService.write(new Article()); ...
java -javaagent:spring-instrument-3.2.0.RELEASE.jar sp32.MainLTW
-- result
successfully weave Aspect into *ArticleService.
2. @EnableLoadTimeWeaving and AspectJ is not worked
-- @Configuration spring config
-- aop.xml and ProfilingAspect are same as XML configurationCode:@Configuration @EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED) public class SpringConfig { @Bean public ReadArticleServiceImpl articleService() { return new ReadArticleServiceImpl(); } .... }
-- Main class
-- runCode:public class MainLTWConfiguration { public static void main(String[] args) throws ArticleNotFoundException { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(SpringConfig.class); context.refresh(); ReadArticleService readArticleService = new ReadArticleServiceImpl(); readArticleService.getArticleAndIncreaseReadCount(10); ...
java -javaagent:spring-instrument-3.2.0.RELEASE.jar sp32.MainLTWConfiguration
-- result
does not weave Aspect into *ArticleService.
Let me know how can i weave aspect into spring bean when i use @EnableLoadTimeWeaving !!
Thanks for reading.
BK


Reply With Quote
