Fork me on GitHub jcabi

24-Oct-2013 0.12

Useful Java AOP Aspects

This module contains a collection of useful AOP aspects, which allow you to modify behavior of a Java application without writing lots of duplicate code.

For example, you may want to retry HTTP resource downloading in case of a failure. You can implement a full do/while cycle yourself or you can annotate the method with @RetryOnFailure and let one of our AOP aspects do the work for you:

public class MyResource {
  @RetryOnFailure
  public String load(URL url) {
    return url.openConnection().getContent();
  }
}

jcabi-aspects works only together with AspectJ, an Aspect Oriented Programming (AOP) framework. At the moment we offer the following aspects (we extend this list every few months):

All you need to do to start using our AOP aspects is to add these two artifacts to your pom.xml:

<dependency>
  <groupId>com.jcabi</groupId>
  <artifactId>jcabi-aspects</artifactId>
  <version>0.12</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.6.12</version>
  <scope>runtime</scope>
</dependency>

Then, we recommend to use our own plugin for AspectJ weaving jcabi-maven-plugin:ajc:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>com.jcabi</groupId>
        <artifactId>jcabi-maven-plugin</artifactId>
        <version>0.8</version>
        <executions>
          <execution>
            <goals>
              <goal>ajc</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Actually, the best way to get all these configurations in one package is to use our com.jcabi:parent parent pom.xml.

However, you can use aspectj-maven-plugin, in order to apply AOP aspects to your code during compile time:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>aspectj-maven-plugin</artifactId>
  <version>1.4</version>
  <configuration>
    <complianceLevel>1.6</complianceLevel>
    <encoding>${project.build.sourceEncoding}</encoding>
    <showWeaveInfo>true</showWeaveInfo>
    <source>1.6</source>
    <target>1.6</target>
    <verbose>true</verbose>
    <aspectLibraries>
      <aspectLibrary>
        <groupId>com.jcabi</groupId>
        <artifactId>jcabi-aspects</artifactId>
      </aspectLibrary>
    </aspectLibraries>
  </configuration>
  <executions>
    <execution>
      <id>weave-classes</id>
      <phase>process-classes</phase>
      <goals>
        <goal>compile</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Also, read more about custom integration of JSR-303 annotations and AOP.

Cutting Edge Version

If you want to use current version of the product, you can do it with this configuration in your pom.xml:

<repositories>
  <repository>
    <id>oss.sonatype.org</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
  </repository>
</repositories>
<dependencies>
  <dependency>
    <groupId>com.jcabi</groupId>
    <artifactId>jcabi-aspects</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
</dependencies>

Copyright © 2012-2013 jcabi.com, All Rights Reserved. Privacy Policy.

site is built by Apache Maven