AOP on Spring DM

Intro

This is a quick introduction how to use Aspect Oriented Programming feature in Spring.

Setup

You need the following bundles to use AspectJ style AOP on Spring DM:

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>com.springsource.org.aspectj.weaver</artifactId>
      <version>1.6.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>com.springsource.org.aspectj.runtime</artifactId>
      <version>1.6.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>net.sourceforge.cglib</groupId>
      <artifactId>com.springsource.net.sf.cglib</artifactId>
      <version>2.1.3</version>
    </dependency>

Also, do not forget dependency for Spring AOP feature:

    <dependency>
      <groupId>org.aopalliance</groupId>
      <artifactId>com.springsource.org.aopalliance</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>2.5.5</version>
    </dependency>

BND setting

AOP features call some of the classes implicitly. Even if you include the dependency above in your pom.xml, BND tool cannot create correct Import-Package section for your manifest file. You need to write the AOP dependency explicitly like the following:

Import-Package: *
DynamicImport-Package: org.aspectj.*, org.springframework.aop.*, net.sf.cglib.*, org.aopalliance.*
Private-Package: org.cytoscape.io.read.internal.*,org.cytoscape.io.write.internal.*, org.cytoscape.io.internal.*

Now you are ready to use AOP on Spring DM.

Example

First, write a very simple aspect:

   1 package org.cytoscape.io.internal.aspects;
   2 
   3 import org.aspectj.lang.JoinPoint;
   4 import org.aspectj.lang.annotation.After;
   5 import org.aspectj.lang.annotation.Aspect;
   6 import org.aspectj.lang.annotation.Before;
   7 
   8 @Aspect
   9 public class SimpleAspect {
  10 
  11         long start;
  12 
  13         @Before("execution(* read*(..))")
  14         public void testAspect(JoinPoint joinPoint) {
  15                 System.out.println("=========== Performance test for "
  16                                 + joinPoint.getSignature().toLongString() + " ==============");
  17                 start = System.currentTimeMillis();
  18         }
  19 
  20         @After("execution(* read*(..))")
  21         public void testAspect2(JoinPoint joinPoint) {
  22                 long time = System.currentTimeMillis() - start;
  23                 System.out.println("=========== Loading time = " + time
  24                                 + " msec. ==============");
  25         }
  26 }
  27 

This aspect means the two methods in the class will be called every time method begin with "read" are executed.

In the bean definition file, you need to define this aspect as a bean. Name for the aspect bean is optional.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:lang="http://www.springframework.org/schema/lang" xmlns:osgi="http://www.springframework.org/schema/osgi"
        xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
            http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.0.xsd"
        default-lazy-init="false">

        <aop:aspectj-autoproxy />
        
        <bean class="org.cytoscape.io.internal.aspects.SimpleAspect" />

...

</beans>

Issues

(not finished yet)

Outdated_Cytoscape_3.0/Developer/AOP (last edited 2011-02-24 16:16:34 by PietMolenaar)

Funding for Cytoscape is provided by a federal grant from the U.S. National Institute of General Medical Sciences (NIGMS) of the Na tional Institutes of Health (NIH) under award number GM070743-01. Corporate funding is provided through a contract from Unilever PLC.

MoinMoin Appliance - Powered by TurnKey Linux