Results 1 to 3 of 3

Thread: Inyecting dependencies on an AspectJ defined aspect

  1. #1
    Join Date
    Dec 2007
    Posts
    130

    Default Inyecting dependencies on an AspectJ defined aspect

    I am learning about load time weaving possibilities. I know it is possible to inject dependencies in woven beans, but is it possible to inject dependencies on Aspects declared in aop.xml? I am just using the example in the reference manual:

    The aspect:


    Code:
    package foo;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.util.StopWatch;
    
    @Aspect
    public class ProfilingAspect {
    
        private StopWatch sw = new StopWatch(getClass().getSimpleName());	
        private String myName = null;
    	
        @Around("methodsToBeProfiled()")
        public Object profile(ProceedingJoinPoint pjp) throws Throwable {
            
            try {
                sw.start(pjp.getSignature().getName());
                return pjp.proceed();
            } finally {
                sw.stop();
                System.out.println(sw.prettyPrint());
            }
        }
    
        @Pointcut("execution(public * foo..*.*(..))")
        public void methodsToBeProfiled(){}
    
        public void setMyName(String myName) {
    	this.myName= myName;
        }
    }
    The beans.xml

    Code:
    <?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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
        <!-- this switches on the load-time weaving -->
        <context:load-time-weaver aspectj-weaving="on"/>    
    </beans>
    The aop.xml
    Code:
    <!DOCTYPE aspectj PUBLIC
            "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
    <aspectj>
        <weaver>
            <include within="foo.*"/>
        </weaver>
        <aspects>                
            <aspect name="foo.ProfilingAspect" />
        </aspects>
    </aspectj>
    The Main.java file to load the context:
    Code:
    package foo;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public final class Main {
    
        public static void main(String[] args) {
    
            ApplicationContext ctx = 
                new ClassPathXmlApplicationContext("/beans.xml", Main.class);
    
            EntitlementCalculationService entitlementCalculationService = 
                  new StubEntitlementCalculationService();
           	 entitlementCalculationService.calculateEntitlement();
        }
    }
    I'd like to know if there is any way inject dependencies in the profilinig aspect. I see no obvious way as the aspect is not in spring-config.xml

  2. #2
    Join Date
    Dec 2007
    Location
    Belgium
    Posts
    24

    Default

    I did this with real aspectJ aspects by calling the factory-method="aspectOf" on the bean to inject property on.
    I'm not sure this works also with aspectJ annotated pojos.

  3. #3
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by controlix View Post
    I did this with real aspectJ aspects by calling the factory-method="aspectOf" on the bean to inject property on.
    I'm not sure this works also with aspectJ annotated pojos.
    You can declare ProfilingAspect bean from your example at the spring context and provide it with necessary dependencies. I.e. you can provide @AspectJ pojos with spring-resolved dependencies.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •