29 August 2017

AspectJ based Spring AOP application

To build a Spring AOP application,We need to setup the following software

1.JDK
2.Apache Maven
3.Intellij/Eclipse(Editor tool)
4.Apache Tomcat

Project structure looks like
spring-aop

Define web.xml file.Declare ContextLoaderListener and provide context-param as spring context xml configuration file (context.xml).

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"                   
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                   
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:context.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

context.xml:

This configuration file contains component scan configuration to detect spring components.
<aop:aspectj-autoproxy/> statement is to support AOP.
<beans xmlns="http://www.springframework.org/schema/beans"       
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
xmlns:context="http://www.springframework.org/schema/context"      
xmlns:aop="http://www.springframework.org/schema/aop"       
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd     
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context.xsd    
                    http://www.springframework.org/schema/aop     
                    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="become.java9pro"/>
 </beans>

Define  Employee  class.
package become.java9pro.aop;

import org.springframework.stereotype.Component;

@Componentpublic class Employee {

    String employeeName;
    Long employeeId;

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) throws InterruptedException {
        Thread.sleep(500);
        this.employeeName = employeeName;
    }

    public Long getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(Long employeeId) throws InterruptedException {
        Thread.sleep(500);
        this.employeeId = employeeId;
    }

}
Define  AopTester  class.
package become.java9pro.aop;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Componentpublic class AopTester implements InitializingBean {

    @Autowired    Employee employee;


    public void afterPropertiesSet() throws Exception {
        employee.setEmployeeName("Java10Pro");
        Thread.sleep(1000);
        employee.setEmployeeId(100L);

    }
}
Define  MethodTimeCalculator  class. logTime method defined  in this class has Around advice and it will be executed around all the method executions in become.java9pro.aop package.

package become.java9pro.aop;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


@Component@Aspectpublic class MethodTimeCalculator {
    Logger logger = Logger.getLogger(MethodTimeCalculator.class);

    @Around("execution(* become.java9pro.aop.*.*(..))")
    public void logTime(ProceedingJoinPoint jp){
        long startTime = System.currentTimeMillis();
        try {
            jp.proceed();

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
       
       long executionTime =System.currentTimeMillis()-startTime;
       logger.info("Execution Time for "+jp.getSignature().getName()+" is "+executionTime);

    }

}

Define log4j configuration.

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" ><log4j:configuration>

    <appender name="stdout1" class="org.apache.log4j.FileAppender">
        <param name="file" value="methodTimeLog.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
        </layout>
    </appender>
    <logger name="become.java9pro.aop" additivity="true">
        <level value="debug"/>
        <appender-ref ref="stdout1"/>
    </logger>
</log4j:configuration>

pom.xml look like below.

<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>java9pro-group</groupId>
    <artifactId>aop-artifact</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>
    <build>
        <finalName>springaop</finalName>
    </build>

    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>

    </dependencies>


</project>
Build the application with mvn clean install.War file will be generated in target folder.
Install the war file in tomcat container by placing war file in webapps folder.start the tomcat server.We can see the following output in  log file methodTimeLog.log 
23:40:45,741  INFO MethodTimeCalculator:24 - Execution Time for setEmployeeName is 540
23:40:47,242  INFO MethodTimeCalculator:24 - Execution Time for setEmployeeId is 500.