1

I am having trouble injecting a property into LoggingAspect class. Imaging I have an AspectJ class:

@Aspect
public class LoggingAspect {
   private IBoc theBo;

   /** getter and setter **/
}

This is the BOC:

public interface IBoc {
}

public class BocImpl implements IBoc {
}

and the Spring configuration for BOC:

<beans ...>
   <aop:aspectj-autoproxy/>

   <bean id="theBoc" class="org.huahsin.BocImpl"/>
</beans>

In applicationContext.xml file, I configure the AspectJ in this way:

<beans...>
   <bean id="theLog" class="org.huahsin.LoggingAspect">
      <property name="theBo" ref="theBoc"/>
   </bean>
</beans>

How could I inject theBo in LoggingAspect class?


Update on 17 Oct 2012

I found some clue here. If I remove the <aop:aspectj-autoproxy>, the member variable theBo in class LoggingAspect will not be null. If I have that code, theBo will be null.

huahsin68
  • 6,819
  • 20
  • 79
  • 113

1 Answers1

3

Normally Spring is responsible for both creating and configuring beans. AspectJ aspects, however, are created by the AspectJ runtime. You need Spring to configure the aspect that AspectJ has created. For the most common case of singleton aspects such as yours LoggingAspect aspect above, AspectJ defines an aspectOf() method that returns the aspect instance. You can tell Spring to use the aspectOf() method as a factory method for obtaining the aspect instance.

For e.g.:

  <beans>      
      <bean name="loggingAspect"
        class="org.huahsin.LoggingAspect"
        factory-method="aspectOf">
        <property name="theBo" ref="theBoc"/>
      </bean>

      <bean id="theBoc" class="org.huahsin.BocImpl"/>
  </beans>

UPDATE:

Define factory method in your class:

@Aspect
public class LoggingAspect {

    private IBoc iBoc;

    private static LoggingAspect instance = new LoggingAspect();

    public static LoggingAspect aspectOf() {
        return instance;
    }

    public void setiBoc(IBoc iBoc) {
        this.iBoc = iBoc;
    }
}
Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
  • I got this error -> javax.servlet.ServletException: Error creating bean with name 'loggingAspect' defined in ServletContext resource [/WEB-INF/auditlog-spring-context.xml]: No matching factory method found: factory method 'aspectOf' – huahsin68 Oct 15 '12 at 10:05
  • Try to add the following line into your spring configuration: . Also dont forget to add: – Paulius Matulionis Oct 15 '12 at 10:10
  • Do you have any shortcut on this error? -> Caused by: java.lang.IllegalStateException: ClassLoader [com.ibm.ws.classloading.internal.UnifiedClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:spring-agent.jar – huahsin68 Oct 15 '12 at 10:34
  • OK. remove that load-time-weaver, add annotation to your aspect @Configurable, add in your spring xml and remove factory-method="aspectOf". If this works I will edit the answer. For the above error you can look here: http://www.dotkam.com/2010/10/12/running-spring-aspectj-tests-with-maven/ – Paulius Matulionis Oct 15 '12 at 10:46
  • The Configurable thing not working. Rgd the LoadTimeWeaver issue, I need to find out how could I set specify the `-javaagent` argument in WAS Liberty Profile server because I am not using Tomcat as my local server. – huahsin68 Oct 15 '12 at 10:59
  • 1
    These SO questions refers to the similar problem: http://stackoverflow.com/questions/4541957/spring-unable-to-inject-in-aspect, http://stackoverflow.com/questions/1343952/spring-dependency-injecting-an-annotated-aspect – Paulius Matulionis Oct 15 '12 at 11:07
  • @PauliusMatulionis I though `aspectOf()` was generated automatically. Maybe thats only for `*.aj` files? – Adam Gent Oct 16 '12 at 02:22
  • Actually I have never used aspect my self, I was using simple AOP logging. Try to create aspectOf() method your self how I showed in the answer. – Paulius Matulionis Oct 16 '12 at 07:54