Tuesday, 8 December 2015

spring aop done using annotations:
==========================
//CustomerBO.java
package com.aop;

public interface CustomerBO {
void addCustomer();
String addCustomerReturnValue();
void addCustomerThrowsException() throws Exception;
void addCustomerAround();

}
=================================================
//CustomerBoImpl.java which implements CustomerBO.java

package com.aop;

public class CustomerBOImpl implements CustomerBO {

public void addCustomer() {

System.out.println("inside addCustomer method!!!");
}

public String addCustomerReturnValue() {
System.out.println("inside addCustomerReturnValue!!!");
return "JAVA";
}

public void addCustomerThrowsException() throws Exception{
System.out.println("Inside addCustomerThrowsException method!!!");
throw new Exception("Generic Exception");
}

public void addCustomerAround() {
System.out.println("Inside addCustomerAround method!!!!");

}

}
==========================================

Now i am trying to apply the following services to above methods like addCustomerAround(),
addCustomerThrowsException() like that....etc..which we defined in CustomerDaoImpl which implements CustomerBO



//loggingAspect.java
package com.aop;

import java.util.Date;

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;

@Aspect
public class LoggingAspect {
@Before("execution (* com.aop.CustomerBO.addCustomer(..))")
public void logBefore(JoinPoint point) {
System.out.println(point.getSignature().getName());
System.out.println("Inside logBefore method is executing!!"
+ new Date());

}

@After("execution (* com.aop.CustomerBO.addCustomer(..))")
public void logAfter(JoinPoint point) {
System.out.println(point.getSignature().getName());
System.out
.println("Inside logAfter method is executing!!" + new Date());

}

@AfterReturning(pointcut = "execution(* com.aop.CustomerBO.addCustomerReturnValue(..))", returning = "result")
public void logAfterReturning(JoinPoint point, Object result) {
System.out.println("result::" + result);
System.out.println("Inside logAfterReturning() method is executing!!!"
+ new Date());

}
@AfterThrowing(pointcut = "execution(* com.aop.CustomerBO.addCustomerThrowsException(..))", throwing = "error")
public void logAfterThrowing(JoinPoint point, Throwable error) {
System.out.println("result::" + error);
System.out.println("Inside logAfterThrowing() method is executing!!!"
+ new Date());

}
@Around("execution(* com.aop.CustomerBO.addCustomerAround(..))")
public void logAround(ProceedingJoinPoint point) throws Throwable
{
System.out.println("logBefore() is executing!!!");
point.proceed();//actual method i.e addCustomerAround()
System.out.println("logAfter() is executing!!!");
}
}

Whenver we are applying the AOP through Annotations we no need to configure in xml file (i.e spring xml file)

just we need to configure Business implementation class and aswell as LoggingAspect classes thats it.

//spring-beans.xml

<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-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-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="custBo" class="com.aop.CustomerBOImpl"></bean>
<bean id="logAspect" class="com.aop.LoggingAspect"></bean>
</beans>


Now the main application we have to do according to our requirements


//App.java

package com.aop;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class App {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = new FileSystemXmlApplicationContext(
"classpath:spring-beans.xml");
CustomerBO customerBO = (CustomerBO) context.getBean("custBo");
//customerBO.addCustomer();
//customerBO.addCustomerReturnValue();
// customerBO.addCustomerThrowsException();
customerBO.addCustomerAround();

}
}

I hope what i posted it might be very usefull to you and give your valuable comments..

Ram here..

Thanks