Announcement Announcement Module
Collapse
No announcement yet.
Help with AspectJ load time weaving injection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with AspectJ load time weaving injection

    I have some working aspects and I wanted to inject a factory object into one of them. These aspects have been working great and the factory object is injected into other non-aspect classes without any problems. I'm getting errors when trying to inject the object into an aspect. Here is what my code looks like below and the error I'm getting is at the bottom. I've been beating my head against a wall all day, pouring through forum posts and I can't figure out what I'm doing wrong. Hopefully of you will be able to help me.

    aop.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <aspectj>
        <aspects>
            <!-- Declare aspects to be used by the service -->
            <aspect name="com.xyz.myservice.aspects.LoggingAspect" />
        </aspects>
    
        <weaver options="-verbose -showWeaveInfo">
            <include within="com.xyz.myservice..*" /> <!-- REQUIRED -->
        </weaver>
    </aspectj>
    application-context.xml
    Code:
         ...... 
         ......
        <context:load-time-weaver aspectj-weaving="on"/>
        <bean id="LoggingAspect"    class="com.xyz.myservice.aspects.LoggingAspect" 
           factory-method="aspectOf">
             <property name="someFactory" ref="SomeFactory" />
        </bean>
    LoggingAspect.java
    Code:
    @Aspect
    public class LoggingAspect{
       
        private SomeFactory someFactory;
    
        public void setSomeFactory(SomeFactory someFactory) {
            this.someFactory = someFactory;
        }
    
        followed by pointcuts and advice...
    When starting the JVM I'm giving it the following arguments:
    -javaagent:myPath/lib/spring-instrument-3.0.jar />
    -javaagent:myPath/lib/aspectjweaver-1.6.12.jar/>

    The server output looks like this.

    1) AspectJ comes up and registers the aspects I have defined in aop.xml
    2) I see lots of output for the weave info of the classes it's weaving during load time.
    3) AspectJ finishes and the server starts up.
    4) As soon as the server starts it fails trying to create the LoggingAspect bean. The error looks like this.

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'LoggingAspect' defined in file [/path/to/service/application-context.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'someFactory' of bean class [com.xyz.myservice.aspects.LoggingAspect]: Bean property 'someFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

    The bean has a setter and it's of the correct type. Any idea what I might be doing wrong here or things I can try?

    Thanks,
    Aaron
    Last edited by arondee; Aug 29th, 2012, 03:58 AM.

  • #2
    There seems to be problem with use of factory-method. You should not be injecting factory in the class, instead should be using the factory-method to call on factory method that will create instance that will be injected. i:e

    Code:
            <bean id="someBean" factory-method="factoryMethod" class="some.factory.class">
    	</bean>
            
             <bean id="LoggingAspect"    class="com.xyz.myservice.aspects.LoggingAspect" 
                    <property name="someFactory" ref="someBean" />
             </bean>

    Comment

    Working...
    X