Basic information

Creating own aspect

There is module wiseporter-logging (not from OpenHub framework) that contains aspect com.openwise.wiseporter.log.EntitySavingAspect - this aspect logs entity before saving:

/**
 * Aspect example.
 *
 * @author <a href="mailto:petr.juza@openwise.cz">Petr Juza</a>
 * @since 0.1
 */
@Aspect
@Configurable
public class EntitySavingAspect {

    @Autowired
    private ApplicationContext ctx;

   public EntitySavingAspect() {
      System.out.println("EntitySavingAspect created ...");
   }

    @Before("com.openwise.wiseporter.commons.aop.CommonPointcuts.preSavingEntity()")
    public void logBeforeSaving(JoinPoint jp) {
        System.out.println("AOP (" + (ctx != null) + "): " + jp.getSignature());
    }
}

where is reference to CommonPointcuts class (module wiseporter-commons):

@Aspect
public class CommonPointcuts {

    /**
     * Join point is pre-saving method in entities.
     */
    @Pointcut(value = "execution(void com.openwise.wiseporter.coreapi.entity.Saveable+.preSave())")
    public void preSavingEntity() {}

    /**
     * Join point is post-saving method in entities.
     */
    @Pointcut(value = "execution(void com.openwise.wiseporter.coreapi.entity.Saveable+.postSave())")
    public void postSavingEntity() {}

    /**
     * Join point is deleting method in entities.
     */
    @Pointcut(value = "execution(void com.openwise.wiseporter.coreapi.entity.Deletable+.delete())")
    public void inDeletingEntity() {}
}


It's necessary to adjust AspectJ compilation to use this aspect in another modules. We need to add dependency to this module with aspect - see adding wiseporter-logging module into aspectLibraries below.

<build>
    <plugins>
        <!-- added dependency to wiseporter-logging -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                    <aspectLibrary>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>wiseporter-logging</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
        </plugin>
     </plugins>
</build>


If aspect itself should be Spring bean (e.g. I need to use auto-wiring to another beans) then aspect has to be marked by annotation @Configurable.

Common AOP hints

Tips for troubleshooting:

Useful links


See Getting started to know how set up IDE for AspectJ compilation.