views:

771

answers:

3

I'm having trouble getting AspectJ to perform load time weaving on a class annotated with @configurable in my main project. No fields get set and none of the setters are touched.

I don't think there's trouble with the configuration itself, because I've extracted the configuration and tested it on a smaller sandbox project. Just for the sake of it, I'll include it in this question though.

So, I'm wondering:

  1. Is there anything in the larger project that might be hindering Spring/AspectJ from detecting the this particular class?
  2. Is there any way of checking if spring is even aware of the class in questions?

And lastly, whatever code I can extract (please excuse the obfuscation):

From configuration XML:

<context:annotation-config />
<context:spring-configured />
<context:component-scan base-package="se.isydev" />
<context:component-scan base-package="se.istools" />
<aop:aspectj-autoproxy />
<context:load-time-weaver aspectj-weaving="on" />
<context:property-placeholder location="classpath:settings.properties" />
(...)
<bean class="com.company.ClassToBeWeaved"
 scope="prototype">  
 <property name="injectedBean" ref="injectedBean" />
</bean>

And the class itself:

@Configurable
public class ClassToBeWeaved {
    private InjectedBean injectedBean;

    @Required
    public void setInjectedBean() { ... }
}

Edit:

Well, turns out that it wasn't working due to a circular dependency. Oh deary me, I love working on legacy code. Still, my original questions remain.

A: 

You possibly have forgotten to "weave". Add -javaagent:path/to/aspectjweaver.jar or -javaagent:path/to/spring-agent.jar to your comman line.

I also suggest that you @Autowire your dependency rather than explicitly inject it.

Paul McKenzie
Nope, not that either. -javaagent:(...)/spring-agent-2.5.6.jar" is on my command line.
mikek
A: 

I believe the LTW requires a META-INF/aop.xml on your classpath. It should look like:

<aspectj>
    <!--
     Uncomment this is you need AOP logging <weaver options="-verbose
     -showWeaveInfo
     -XmessageHandlerClass:org.springframework.aop.aspectj.AspectJWeaverMessageHandler">
    -->
    <weaver>
        <include within="com.xxx.MyClass" />
    </weaver>
    <aspects>
     <aspect name="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"/>
        <include within="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"/>
    </aspects>
</aspectj>
Kevin
Ah nope, doesn't require it. As I said, it works dandy in my sandbox.
mikek
A: 

Do you have more then 1 spring XML file? I believe I had a problem <aop:aspectj-autoproxy /> was not in the 'most parent' of my XML files.

festerwim
See my edit. The error was due to a circular dependency in the code (not mine).
mikek