Announcement Announcement Module
Collapse
No announcement yet.
@EnableLoadTimeWeaving is not working? but <context:load-time-weaver /> is worked!. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @EnableLoadTimeWeaving is not working? but <context:load-time-weaver /> is worked!.

    I use Spring 3.2.0.

    1. <context:load-time-weaver> and AspectJ is worked

    -- XML configuration
    Code:
    <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" />
    ...
    -- META-INF/aop.xml
    Code:
    <aspectj>
    	<weaver>
    		<include within="sp32.board.service.*" />
    		<include within="sp32.aop.annot.*" />
    	</weaver>
    
    	<aspects>
    		<aspect name="sp32.aop.annot.ProfilingAspect" />
    	</aspects>
    </aspectj>
    -- ProfilingAspect class
    Code:
    @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");
    		}
    	}
    }
    -- Main class
    Code:
    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());
    		...
    -- run
    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
    Code:
    @Configuration
    @EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
    public class SpringConfig {
    	
    	@Bean
    	public ReadArticleServiceImpl articleService() {
    		return new ReadArticleServiceImpl();
    	}
    ....
    }
    -- aop.xml and ProfilingAspect are same as XML configuration

    -- Main class
    Code:
    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);
    ...
    -- run
    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

  • #2
    Due to the usage of a class instead of xml your classes are already loaded before loadtimeweaving is applied and once a class is loaded it cannot be changed anymore. One thing you could try is to create 2 config classes one with @EnableLoadTimeWeaving and one with the bean definitions. Then make sure the @EnableLoadtimeWeaving one is loaded first.

    Comment

    Working...
    X