Announcement Announcement Module
Collapse
No announcement yet.
Inyecting dependencies on an AspectJ defined aspect Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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.

      Comment

      Working...
      X