Spring AOP Annotation Project

Click here to download eclipse supported ZIP file



This is CustomerBo.java file having the source code to execute business logic.


 

    
package com.cv.spring.aop.annotation;
/**
 @author Chandra Vardhan
 */
public interface CustomerBo {

  void addCustomer();

  String addCustomerReturnValue();

  void addCustomerThrowException() throws Exception;

  void addCustomerAround(String name);
}


This is CustomerBoImpl.java file having the source code to execute business logic.


 

    
package com.cv.spring.aop.annotation;

import org.apache.log4j.Logger;

/**
 @author Chandra Vardhan
 */
public class CustomerBoImpl implements CustomerBo {
  
  private final static Logger LOGGER = Logger.getLogger(CustomerBoImpl.class);

  public void addCustomer() {
    LOGGER.info("addCustomer() is running ");
  }

  public String addCustomerReturnValue() {
    LOGGER.info("addCustomerReturnValue() is running ");
    return "abc";
  }

  public void addCustomerThrowException() throws Exception {
    LOGGER.info("addCustomerThrowException() is running ");
    throw new Exception("Generic Error");
  }

  public void addCustomerAround(String name) {
    LOGGER.info("addCustomerAround() is running, args : " + name);
  }
}


This is LoggingAspect.java file having the source code to execute business logic.


 

    
package com.cv.spring.aop.annotation;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
/**
 @author Chandra Vardhan
 */
@Aspect
public class LoggingAspect {
  private final static Logger LOGGER = Logger.getLogger(LoggingAspect.class);
  @Pointcut("execution(* com.cv.spring.aop.annotation.CustomerBoImpl.addCustomer(..))")
  public void performance() {
  }

  @Before("execution(* com.cv.spring.aop.annotation.CustomerBoImpl.addCustomer(..))")
  public void logBefore() {
    LOGGER.info("*********************************************");
    System.out
        .println("logBefore() is called before actual method execute...");

    LOGGER.info("**********************************************");
  }

  @After("execution(* com.cv.spring.aop.annotation.CustomerBoImpl.addCustomer(..))")
  public void logAfter() {
    LOGGER.info("*********************************************");
    System.out
        .println("logAfter() is called After actual method execute...");

    LOGGER.info("**********************************************");
  }

  @Before("execution(* com.cv.spring.aop.annotation.CustomerBoImpl.addCustomer(..)) && !bean(logAspect)")
  public void logBefore(JoinPoint joinPoint) {
    LOGGER.info("*********************************************");
    System.out
        .println("logBefore() is called before actual method execute...");
    LOGGER.info("hijacked : " + joinPoint.getSignature().getName());
    LOGGER.info("**********************************************");
  }

  @After("execution(* com.cv.spring.aop.annotation.CustomerBoImpl.addCustomer(..))")
  public void logAfter(JoinPoint joinPoint) {
    LOGGER.info("logAfter() is running!");
    LOGGER.info("hijacked : " + joinPoint.getSignature().getName());
    LOGGER.info("******");
  }

  @AfterReturning(pointcut = "execution(* com.cv.spring.aop.annotation.CustomerBo.addCustomerReturnValue(..))", returning = "result")
  public void logAfterReturning(JoinPoint joinPoint, Object result) {
    LOGGER.info("logAfterReturning() is running!");
    LOGGER.info("hijacked : " + joinPoint.getSignature().getName());
    LOGGER.info("Method returned value is : " + result);
    LOGGER.info("******");
  }

  @Around("execution(* com.cv.spring.aop.annotation.CustomerBo.addCustomerAround(..))")
  public void logAround(ProceedingJoinPoint joinPointthrows Throwable {
    LOGGER.info("logAround() is running!");
    LOGGER.info("hijacked method : "
        + joinPoint.getSignature().getName());
    LOGGER.info("hijacked arguments : "
        + Arrays.toString(joinPoint.getArgs()));
    LOGGER.info("Around before is running!");
    joinPoint.proceed();
    LOGGER.info("Around after is running!");
    LOGGER.info("******");
  }

  @AfterThrowing(pointcut = "execution(* com.cv.spring.aop.annotation.CustomerBo.addCustomerThrowException(..))", throwing = "error")
  public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
    LOGGER.info("logAfterThrowing() is running!");
    LOGGER.info("hijacked : " + joinPoint.getSignature().getName());
    LOGGER.info("Exception : " + error);
    LOGGER.info("******");
  }
}


This is MainProgram.java file having the source code to execute business logic.


 

    
package com.cv.spring.aop.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 @author Chandra Vardhan
 */
public class MainProgram {
  
  public static void main(String[] argsthrows Exception {
    ApplicationContext appContext = new ClassPathXmlApplicationContext(
        "spring-config.xml");
    CustomerBo customerBoImpl = (CustomerBoappContext.getBean("customerBo");    
    customerBoImpl.addCustomer();    
  }
}




This is pom.xml file having the entries of dependency jars and information to build the application .


	
<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">.0" encoding="UTF-8"?> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.spring.annotation</groupId> <artifactId>SpringAOPAnnotation</artifactId> <name>SpringAOPAnnotation</name> <packaging>jar</packaging> <version>1.0</version> <properties> <java-version>1.8</java-version> <log4j-version>1.2.16</log4j-version> <org.springframework-version>4.1.6.RELEASE</org.springframework-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.6</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>


This is spring-config.xml file having the spring configuration properties.


 

<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="customerBo" class="com.cv.spring.aop.annotation.CustomerBoImpl" />
<!-- Aspect -->
<bean id="logAspect" class="com.cv.spring.aop.annotation.LoggingAspect" />

</beans>



This is log4j.properties file having the entries for logging the information into the console/file.



#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


No comments:

Post a Comment