Announcement Announcement Module
Collapse
No announcement yet.
@Configurable @Aspect - AspectJ aspect configured by spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Configurable @Aspect - AspectJ aspect configured by spring

    Hello is this a way how I can configure aspect witch spring?

    I have this aspect:
    Code:
    @Aspect
    @Configurable
    public class TempletAspect {
    
      /** Describe templet here. */
      @Autowired
      @Qualifier("templet")
      private ITemplet templet;
    
      /** Get the <code>Templet</code> value.
       * @return a value
       */
      public final ITemplet getTemplet() {
        return templet;
      }
    
      /** Set the <code>Templet</code> value.
       * @param newTemplet The new Templet value.
       */
      public final void setTemplet(final ITemplet newTemplet) {
        this.templet = newTemplet;
      }
    
      @Around("target(cz.apnetis.web.INeedTemplet) && call(* *.getTemplet())")
      public final Object setTempletToController(final ProceedingJoinPoint
                                                 thisJoinPoint) {
        System.out.println("ASPECT IS CALLL --------------------");
        return getTemplet();
      }
    }
    Aspect is call but propertie ITemplet isn't set and this exception raise:
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cz.apnetis.web.support.TempletAspect': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cz.apnetis.web.ITemplet cz.apnetis.web.support.TempletAspect.templet; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [cz.apnetis.web.ITemplet] is defined: Unsatisfied dependency of type [interface cz.apnetis.web.ITemplet]: expected at least 1 matching bean
    But I have templet which implements ITemplet in my spring context.

    Its there a way to mixing AspectJ and Spring in one aspect?

  • #2
    Why do you want your bean to be @Configurable?! Do you plan on creating new instances of the TempletAspect in your code? At least that is what the @Configurable is used for, next to that I don't see the added value on an @Aspect bean. So remove it.

    Next to that please post your whole stacktrace so that we can see the full message. Make sure that there is a bean of type ITemplet with the name 'templet' in your configuration.

    Comment


    • #3
      Why do you want your bean to be @Configurable?! Do you plan on creating new instances of the TempletAspect in your code? At least that is what the @Configurable is used for, next to that I don't see the added value on an @Aspect bean. So remove it.

      Next to that please post your whole stacktrace so that we can see the full message. Make sure that there is a bean of type ITemplet with the name 'templet' in your configuration.

      Comment


      • #4
        Originally posted by mdeinum View Post
        Why do you want your bean to be @Configurable?! Do you plan on creating new instances of the TempletAspect in your code? At least that is what the @Configurable is used for, next to that I don't see the added value on an @Aspect bean. So remove it.

        Next to that please post your whole stacktrace so that we can see the full message. Make sure that there is a bean of type ITemplet with the name 'templet' in your configuration.
        Now I use aspects which configure my Controller (web controller), and add templet (which is wrapper, but it's hidden) to all Controller which is in application.

        When I Use spring configured aspect I can write this:
        Code:
        <bean id="templetAspect" class="cz.ap.....TempletAspect">
          <property name="parametr" ref="someBean" />
        </bean>
        Yes, it's spring configurable aspect. But when I use aspect configurable bean, then I cannot use configurable aspect. Because of @Configurable and @Aspect in one bean cannot be used.

        Yes, it's another way. To anotatte all controller with @Configurable, and templet which is set by templet aspect will be set directly by spring. But when I inherit from class which have some property anotated with @Autowire(required = true), then i cannot use spring context, to set this properties.

        On example, when I anotate witch @Configure class which inherite from HiberanteSuport then I must have SessionFactory and HibernateProperty set by annotation, not only from spring context.

        Yes, it's conflict between spring configurable and aspectj AOP. I need spring configurable Aspect. But I want use my aspect in all aplication, not only in bean which create spring. Because of I want make aspect, which can be configured from spring.

        Comment


        • #5
          Again WHY does your Aspect have to be @Configurable??? I strongly suggest you read the chapter about @Configurable and also the chapter about annotation based configuration. For annotation based configuration yuo don't need the @Configurable.

          Comment


          • #6
            Originally posted by mdeinum View Post
            Again WHY does your Aspect have to be @Configurable??? I strongly suggest you read the chapter about @Configurable and also the chapter about annotation based configuration. For annotation based configuration yuo don't need the @Configurable.
            Yes I needn't use @Configurable but I need @Autowired, becouse I want set parameter to annotation. But I don't know if compiled anntotation is call as new instance of class, or is add around the method, and defacto is wired to target class and no instance of aspect class is create.

            Comment


            • #7
              Workaround

              Originally posted by benzin View Post
              Its there a way to mixing AspectJ and Spring in one aspect?
              Hi, I solved this question using CTW (Compile-time Weaving) on the Annotation @Configure. I followed the Chris Searle Blog entry (http://www.chrissearle.org/blog/tech...ng_using_maven) about CTW using Maven.

              Added this entry on maven:

              Code:
                          <plugin>
                              <groupId>org.codehaus.mojo</groupId>
                              <artifactId>aspectj-maven-plugin</artifactId>
                              <configuration>
                                  <complianceLevel>1.5</complianceLevel>
                                  <aspectLibraries>
                                      <aspectLibrary>
                                          <groupId>org.springframework</groupId>
                                          <artifactId>spring-aspects</artifactId>
                                      </aspectLibrary>
                                  </aspectLibraries>
                              </configuration>
                              <executions>
                                  <execution>
                                      <goals>
                                          <goal>compile</goal>
                                      </goals>
                                  </execution>
                              </executions>
                          </plugin>
              And Kept the Load Time Weaving for the AspectJ Annotation.

              BTW, do you really need this? I guess Marten Deinum is right about it.
              Last edited by RRantz; Jul 21st, 2009, 09:34 PM. Reason: grammar errors

              Comment

              Working...
              X