Announcement Announcement Module
Collapse
No announcement yet.
Dependency Injection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dependency Injection

    Is there a way to inject spring beans into aspects defined in my aop.xml file (I'm using load time weaving)? I'd assume not but I thought I'd ask...

  • #2
    It is possible to inject dependencies in aspects defined in aop.xml. The mechanism is the same as aspects woven using any other weaving styles.

    -Ramnivas

    Comment


    • #3
      I'm confused as to what you mean by that. In my aop.xml file, I have something that looks like
      Code:
      <aspectj>
        <aspects>
           <aspects>
                <aspect name="my.Aspect"/>
            </aspects>
        </aspects>
      
        <weaver>
           ...some weaver code...
        </weaver>
      </aspectj>
      In this case, I have a class my.Aspect annotated with @Aspect.
      I'd like to have my.Aspect be a bean configured in my spring bean definition file because I'd like to be able to configure its parameters.

      I'm not quite sure I understand how to do this. Isn't my.Aspect the fully qualified class name of the aspect? If so, wouldn't this just take a new instance rather than getting the configured bean version?

      Comment


      • #4
        You still do:

        Code:
        <bean id="..." class="my.Aspect" factory-method="aspectOf">
            ... properties
        </bean>
        Try this out.

        -Ramnivas

        Comment


        • #5
          Re: Dependency Injection

          I tried that method, and I get the error saying "No matching factory method found: factory method aspectOf", but I think I see what's going on. I am not compiling with an AspectJ compiler, so my guess is I should be using Aspects.aspectOf in my code to get access to this object.

          But, I'm not sure this solves my problem. I think I may have misstated what I'm trying to do. Basically, when the aop.xml file looks up the aspect, I want to be able to have configured that aspect (using constructor injection) through my spring xml file. The reason is the aspect takes a list object and its behavior at join points is based on what is in that list. I didn't want to have to recompile the code to change that list. Is this possible?
          Last edited by jeffastorey; Nov 30th, 2007, 10:21 AM.

          Comment


          • #6
            Are you able to get advice applied for a simple aspect without dependency injection (perhaps just print a statement)? I suggest that you try to get that working first.

            -Ramnivas

            Comment


            • #7
              Re: Dependency Injection

              When I compile the class with the @Aspect annotation and don't try to create it in a bean definition file, it works with no problem.

              What I've done for now (and maybe this is the correct solution) is created a bean definition file that has the parameters I need and then no-arg constructor of the @Aspect loads them from the bean file.

              Comment


              • #8
                Originally posted by jeffastorey View Post
                What I've done for now (and maybe this is the correct solution) is created a bean definition file that has the parameters I need and then no-arg constructor of the @Aspect loads them from the bean file.
                Hi jeffastorey! I have exactly the same problem configuring load-time-woven aspects with dependency injection. In my aspect-object I need a reference to a DAO, which I have configured in spring's context.xml. Is this possible? Would you please show me how you did it?
                Thanks

                Comment


                • #9
                  thofoer,

                  I did something like this.

                  Code:
                  @Aspect
                  public class MyAspect {
                  
                     private final DAO myDAO;
                  
                     public MyAspect() {
                       ApplicationContext ctx = new FileSystemXmlApplicationContext("data/mybeans.xml");
                       myDAO = (DAO) ctx.getBean("MyDAO");
                     }
                  
                  }
                  Hope this helps,

                  Jeff

                  Comment


                  • #10
                    Thank you for the answer. But in this way I would create a second application context within the aspect-object (in addition to that one in the main program). I'm not sure if this can cause problems, but anyway - all my beans (DAOs, Hibernate template, data source etc.) will be created twice.
                    Alternatively I consider to assign the DAO to the aspect with a global, public, static method somewhere (not very nice, not very OO). Maybe we find a better solution sometime...

                    Comment


                    • #11
                      Yes, you do end up creating a second context, but if all of your beans are lazy-init, it doesn't really cause a problem (unless of course your aspect is trying to reference other beans in the other context).

                      Comment

                      Working...
                      X