Announcement Announcement Module
Collapse
No announcement yet.
Can I access applicationContext from AspectJ ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can I access applicationContext from AspectJ ?

    Hi !!

    I've tested Spring 2.5-rc2 load time weaving. But I don't find out how to access spring bean(which is defined in applicationContext) from Aspect.

    Any comments will be helpful ^^

  • #2
    You may use the regular dependency injection to inject other beans into aspect. Just make sure that you use aspectOf as the factory method when creating bean for the aspect. See section 6.8.1 of the Spring reference manual for more details.

    Of course, you can make the aspect ApplicationContextAware and get access to the application context. Note that you will still need to have a bean corresponding to the aspect (and follow the use of aspectOf as previously mentioned).

    -Ramnivas

    Comment


    • #3
      Originally posted by ramnivas View Post
      You may use the regular dependency injection to inject other beans into aspect. Just make sure that you use aspectOf as the factory method when creating bean for the aspect. See section 6.8.1 of the Spring reference manual for more details.

      Of course, you can make the aspect ApplicationContextAware and get access to the application context. Note that you will still need to have a bean corresponding to the aspect (and follow the use of aspectOf as previously mentioned).

      -Ramnivas
      Do you need to use the factory-method='aspectOf' if you are using @Aspect classes? It seems as though my @Aspect classes are not being injected with relevant beans. I presume you use the factory-method if your using old style aspectj.

      Thanks
      Last edited by amin; Nov 20th, 2007, 07:42 AM. Reason: additional information

      Comment


      • #4
        You need to use aspectOf() with either style.

        -Ramnivas

        Comment


        • #5
          Hi Ramnivas

          Thanks for getting back to me...again! The problem I am having is that I have converted an .aj to a java class and this is giving endless headaches! The old style aspect works fine and spring is injecting the dependencies as expected. It seems as though if i move to the java class i get problems. I have added factory-method="aspectOf" and run the same tests but it keeps failing.

          The reason for moving to annotation style is to LTW with tests and as AJDT can be a pain when developing code.

          Thanks

          Comment


          • #6
            Hi Amin,

            Is it possible to isolate the problem into a standalone test case so that it can be investigated?

            -Ramnivas

            Comment


            • #7
              Hi Ramnivas

              I have found the problem. It looks like that because I am using annotations for my aspect I have to do the following:
              Code:
              public Object advice (ProceedingJoinPoint pjp)  throws Throwable {
              
                 Callback callback = new Callback() {
                    public Object doSomething() {
                          try {
                          }  catch (Throwable th) {
                             throw new RuntimeException(th);
                          }  
                
                   }
                 }
              }
              What was happening before was there was no try catch block and so it would flow to the normal execution where as now it is being re thrown by the aspect rather than being handled by the code.

              Is there a way around this? For example not using Throwable when calling pjp.proceed? probably not!

              Comment


              • #8
                Hi again

                I have figured out what to do...
                Code:
                 public Object proceedWithObject() {
                                    Object result = null;
                                    try {
                                        result = pjp.proceed();
                                    } catch (Throwable th) {
                                        if (log.isDebugEnabled()) {
                                            log.debug("Aspected detected an exception in the joinpoint: " + pjp.toShortString() + ", the exception message is " + th.getMessage());
                                        }
                                    }
                                    return result;
                
                                }
                Getting old!

                Comment


                • #9
                  Hmmm....the above doesn't work. If there is an exception then the object returned is null. The pjp.proceed() should carry on to the normal execution not throw an exception.

                  Comment

                  Working...
                  X