Quick Start with SpringAOP

Limitations:

Due to the popularity of the Spring framework SpringAOP is the most popular AOP framework these days.

SpringAOP has it's limitation such as:

  • It works only with Spring managed beans, i.e you can use @Log annotation only on spring-managed beans.
  • You can not use @Log annotation constructors and methods other than public.
  • Logging of method calls in same Spring managed bean will not work unless you call that method via self reference stored in that bean.

The benefit of SpringAOP is that it unlike AspectJ works with javac compiler.

Maven configuration:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.foo</groupId>
        <artifactId>aspect4log-spring-sample</artifactId>
        <version>1.0.7</version>
        <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <!-- aspect4log can work with java 1.6 and above -->
                <maven.compiler.source>1.6</maven.compiler.source>
                <maven.compiler.target>1.6</maven.compiler.target>
                <!-- the logging is done via slf4j -->
                <slf4j.version>1.7.10</slf4j.version>
                <spring.version>4.1.5.RELEASE</spring.version>
                <aspectj.version>1.8.5</aspectj.version>
        </properties>

        <dependencyManagement>
                <dependencies>
                        <dependency>
                                <groupId>org.springframework</groupId>
                                <artifactId>spring-framework-bom</artifactId>
                                <version>${spring.version}</version>
                                <type>pom</type>
                                <scope>import</scope>
                                <exclusions>
                                        <exclusion>
                                                <artifactId>commons-logging</artifactId>
                                                <groupId>commons-logging</groupId>
                                        </exclusion>
                                </exclusions>
                        </dependency>
                </dependencies>
        </dependencyManagement>

        <dependencies>
                <dependency>
                        <!--We need this because Spring still uses common-logging, jcl-over-slf4j will substitute calles to common-loggin. 
                            Make sure you excluded common-logging in every module that  depends on it. 
                            For more details read http://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/htmlsingle/#overview-logging-slf4j -->
                        <groupId>org.slf4j</groupId>
                        <artifactId>jcl-over-slf4j</artifactId>
                        <version>${slf4j.version}</version>
                </dependency>
                <dependency>
                        <!--we will use logback as logger implementation, it is also possible to log4j and java logging -->
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-classic</artifactId>
                        <version>1.1.2</version>
                </dependency>

                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aop</artifactId>
                </dependency>
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context</artifactId>
                </dependency>
                <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                        <groupId>net.sf.aspect4log</groupId>
                        <artifactId>aspect4log</artifactId>
                        <version>1.0.7</version>
                </dependency>
        </dependencies>
</project>

Spring AOP configuration

@Configuration
@ComponentScan("org.foo")
@EnableAspectJAutoProxy
public class SpringAopConfiguration {

        @Bean
        LogAspect createLogAspect(){
                return new LogAspect();
        }
        
}

Place @Log annotation on any spring bean you have to log it's method's invocation.

Checkout a sample project that demonstrates integration with SpringAOP here https://github.com/yilativs/aspect4log-springaop-sample

Note

We strongly recommend to try integrate aspect4log into your project by means of AspectJ even if the rest of your application uses SpringAOP. Once you started using AspectJ you will find it surprisingly easy in comparison to SprionAOP.