3 Replies Latest reply on Jun 7, 2005 9:02 AM by flaviarnn

    A single AspectFactory across multiple Aspects?

    neil_g_avery

      Hi All,
      Im in the process of hooking up an AspectFactory to our application container. I would like to delegate Aspect creation to our container, given the way AspectFactory hangs together it assumes you will only use a single factory per aspect; in that . Whereas I would like to use the same generic aspectfactory, then determine the aspect context (class) and ask the container for the appropriate aspect.

      One way around this is to write my generic AspectFactory(Class aspectType) and then create a subclass factroy for every aspect type. However this is cumbersome in terms of adding aspects and maintaining the configuration.

      It would be much easier if I were to write my aop.xml as follows,

      <aspect name="Aspect1" class "my.aspect1l" factory="MyJBossAOPFactory" scope="PER_CLASS"/>
      <aspect name="Aspect2" class "my.aspect2" factory="MyJBossAOPFactory" scope="PER_CLASS"/>
      
      <bind pointcut="execution(* my.POJOx->*(..))"/>
       <advice name="execute" aspect="Aspect1"/>
       <advice name="execute" aspect="Aspect2"/>
      <bind/>
      


      Is there a means of using a single AspectFactory across multiple Aspects? (meta data?)
      Regards Neil

        • 1. Re: A single AspectFactory across multiple Aspects?
          flaviarnn

          Hi, Neil,
          I think meta data could do the trick, but there is a simpler way.
          Your aspect factory could implement the org.jboss.util.xml.XmlLoadable interface.
          This interface contains the method
          public void importXml(org.w3c.dom.Element element)
          If you do this, JBoss AOP will call this method after instatiating your Aspect Factory, passing as argument the xml element used to declare your aspect factory in the jboss-aop.xml file.
          This way, your jboss-aop.xml could look like this:

          <aspect name="Aspect1" class "my.aspect1l" factory="MyJBossAOPFactory" scope="PER_CLASS">
           <anyTagy>Aspect1</anyTag>
          </aspect>
          <aspect name="Aspect2" class "my.aspect2" factory="MyJBossAOPFactory" scope="PER_CLASS">
          <anyTag>Aspect2</anyTag>
          </aspect>
          

          And your MyJBossAOPFactory.importXml method could retrieve the "anyTag" contents in order to determine the aspect context (i.e. the class).

          Regards
          Flavia

          • 2. Re: A single AspectFactory across multiple Aspects?
            neil_g_avery

            Excellent - thats exactly what I need,
            Thankyou.

            • 3. Re: A single AspectFactory across multiple Aspects?
              flaviarnn

              You're welcome, but... it seems that I gave you a redundant solution.
              Actually, you don't need to place 'anyTag' inside the 'aspect' element, because you can retrieve the aspect name from your MyJBossAOPFactory.importXml method. If you convention that the aspect name is always the name of the aspect class (the context you want to access), the anyTag is redundant. Just add tags inside aspect elements if it adds useful information to your MyJBossAOPFactory.
              Besides, you should remove the class attribute from the aspect element, otherwise JBoss AOP will throw an exception, warning that you cannot define both class and factory attributes (the first one should be used to define an aspect class, not an aspect factory).
              So, your jboss-aop.xml file should look like this:

              <aspect name="Aspect1" factory="MyJBossAOPFactory" scope="PER_CLASS"/>
              <aspect name="Aspect2" factory="MyJBossAOPFactory" scope="PER_CLASS"/>
              

              Or like this, if you need package information (which probably you do):
              <aspect name="yourPackage.Aspect1" factory="MyJBossAOPFactory" scope="PER_CLASS"/>
              <aspect name="yourPackage.Aspect2" factory="MyJBossAOPFactory" scope="PER_CLASS"/>
              


              Regards
              Flavia