kraken
kraken

Reputation: 493

AspectJ load time weaving isn't work

I want to configure my spring project to work with AspectJ load time weaving. I've done following:

  1. added a <context:load-time-weaver aspectj-weaving="on"/> in my application contex
  2. added aspectj in cache
  3. added lines in my pom.xml

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-instrument</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${org.aspectj.version}</version>
    </dependency>
    ...........
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <goals>
                    <project>test</project>
                </goals>
                <configuration>
                    <detail>true</detail>
                    <forkMode>once</forkMode>
                    <argLine>-javaagent:"${settings.localRepository}/org/springframework/spring-instrument/3.1.4.RELEASE/spring-instrument-3.1.4.RELEASE.jar"</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

I have a test, and it fails.

@Cacheable(value = CacheConstants.FORM_TEMPLATE)
public int getRandomInt(){
    return new Random().nextInt();
}

@Test
public void test(){
    int i = getRandomInt();
    assertEquals(i, getRandomInt());
}

Can somebody explain why it fails? It obvious that load time weaving isn't work.

Upvotes: 1

Views: 238

Answers (1)

kraken
kraken

Reputation: 493

Thanks, M. Deinum! Your comment is very useful! I've rewrited my test as follows

DaoObject

interface DaoObject {
    int getCachedNumber();
}

DaoObjectImpl

@Repository
public class DaoObjectImpl implements DaoObject {
    @Cacheable(value = CacheConstants.FORM_TEMPLATE)
    public int getRandomInt(){
        return new Random().nextInt();
    }

    @Override
    public int getCachedNumber() {
        return getRandomInt();
    }
}

And my test now looks like

@Test
public void cacheTest(){
    assertEquals(daoObject.getCachedNumber(), daoObject.getCachedNumber());
}

All fine. Thanks!

Upvotes: 1

Related Questions