/**
*
*/
package com.doj.aopapp.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* @author Dinesh.Rajput
*
*/
@Aspect
@Component
@Order(0)
public class SecurityAspect {
/**
* Declaring before advice for all transfer methods whose taking three arguments of any type
* of all classes in the package com.doj.aopapp.service
* @param jp
* @throws Throwable
*/
@Before("execution(* com.doj.aopapp.service.*.transfer(*,*,*))")
public void beforeAdviceForTransferMethods(JoinPoint jp) throws Throwable {
System.out.println("****SecurityAspect.beforeAdviceForTransferMethods() " + jp.getSignature().getName());
}
}
/**
*
*/
package com.doj.aopapp.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* @author Dinesh.Rajput
*
*/
@Aspect
@Component
@Order(1)
public class TransactionAspect {
/**
* Declaring before advice for all transfer methods whose taking three arguments of any type
* of all classes in the package com.doj.aopapp.service
* @param jp
* @throws Throwable
*/
@Before("execution(* com.doj.aopapp.service.*.transfer(*,*,*))")
public void beforeAdviceForTransferMethods(JoinPoint jp) throws Throwable {
System.out.println("****TransactionAspect.beforeAdviceForTransferMethods() " + jp.getSignature().getName());
}
}
/**
*
*/
package com.doj.aopapp.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* @author Dinesh.Rajput
*
*/
@Aspect
@Component
@Order(2)
public class LoggingAspect {
/**
* Declaring around advice for all transfer methods whose taking three arguments of any type
* of all classes in the package com.doj.aopapp.service
* @param jp
* @throws Throwable
*/
@Around("execution(* com.doj.aopapp.service.*.transfer(*,*,*))")
public void beforeAdviceForTransferMethods(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("****LoggingAspect Before execution of Transfer Methods " + pjp.getSignature().getName());
pjp.proceed();
System.out.println("****LoggingAspect After execution of Transfer Methods " + pjp.getSignature().getName());
}
}
/**
*
*/
package com.doj.aopapp.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
/**
* @author Dinesh.Rajput
*
*/
@Aspect
@Component
public class SecurityAspect implements Ordered{
/**
* Declaring before advice for all transfer methods whose taking three arguments of any type
* of all classes in the package com.doj.aopapp.service
* @param jp
* @throws Throwable
*/
@Before("execution(* com.doj.aopapp.service.*.transfer(*,*,*))")
public void beforeAdviceForTransferMethods(JoinPoint jp) throws Throwable {
System.out.println("****SecurityAspect.beforeAdviceForTransferMethods() " + jp.getSignature().getName());
}
@Override
public int getOrder() {
return 0;
}
}
/**
*
*/
package com.doj.aopapp.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
/**
* @author Dinesh.Rajput
*
*/
@Aspect
@Component
public class TransactionAspect implements Ordered{
/**
* Declaring before advice for all transfer methods whose taking three arguments of any type
* of all classes in the package com.doj.aopapp.service
* @param jp
* @throws Throwable
*/
@Before("execution(* com.doj.aopapp.service.*.transfer(*,*,*))")
public void beforeAdviceForTransferMethods(JoinPoint jp) throws Throwable {
System.out.println("****TransactionAspect.beforeAdviceForTransferMethods() " + jp.getSignature().getName());
}
@Override
public int getOrder() {
return 1;
}
}
/**
*
*/
package com.doj.aopapp.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
/**
* @author Dinesh.Rajput
*
*/
@Aspect
@Component
public class LoggingAspect implements Ordered {
/**
* Declaring around advice for all transfer methods whose taking three arguments of any type
* of all classes in the package com.doj.aopapp.service
* @param jp
* @throws Throwable
*/
@Around("execution(* com.doj.aopapp.service.*.transfer(*,*,*))")
public void beforeAdviceForTransferMethods(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("****LoggingAspect Before execution of Transfer Methods " + pjp.getSignature().getName());
pjp.proceed();
System.out.println("****LoggingAspect After execution of Transfer Methods " + pjp.getSignature().getName());
}
@Override
public int getOrder() {
return 2;
}
}
/**
*
*/
package com.doj.aopapp.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* @author Dinesh.Rajput
*
*/
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages={"com.doj.aopapp.aspect", "com.doj.aopapp.service"})
public class AppConfig {
}
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <aop:config> <aop:aspect ref="securityAspect" order="0"> <!-- all public methods whose name are transfer() with taking three arguments of any type and any return type of all classes in the com.doj.aopapp.service package --> <aop:pointcut expression="execution(* com.doj.aopapp.service.*.transfer(*,*,*))" id="logForAllTransfer"/> <aop:before method="beforeAdviceForTransferMethods" pointcut-ref="logForAllTransfer"/> </aop:aspect> <aop:aspect ref="transactionAspect" order="1"> <!-- all public methods whose name are transfer() with taking three arguments of any type and any return type of all classes in the com.doj.aopapp.service package --> <aop:pointcut expression="execution(* com.doj.aopapp.service.*.transfer(*,*,*))" id="logForAllTransfer"/> <aop:before method="beforeAdviceForTransferMethods" pointcut-ref="logForAllTransfer"/> </aop:aspect> <aop:aspect ref="loggingAspect" order="2"> <!-- all public methods whose name are transfer() with taking three arguments of any type and any return type of all classes in the com.doj.aopapp.service package --> <aop:pointcut expression="execution(* com.doj.aopapp.service.*.transfer(*,*,*))" id="logForAllTransfer"/> <aop:around method="beforeAdviceForTransferMethods" pointcut-ref="logForAllTransfer"/> </aop:aspect> </aop:config> <bean id="transferService" class="com.doj.aopapp.service.TransferServiceImpl"/> <bean id="securityAspect" class="com.doj.aopapp.aspect.SecurityAspect"/> <bean id="transactionAspect" class="com.doj.aopapp.aspect.TransactionAspect"/> <bean id="loggingAspect" class="com.doj.aopapp.aspect.LoggingAspect"/> </beans>
/**
*
*/
package com.doj.aopapp.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.doj.aopapp.config.AppConfig;
import com.doj.aopapp.service.TransferService;
/**
* @author Dinesh.Rajput
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
TransferService transferService = applicationContext.getBean(TransferService.class);
transferService.transfer("accountA", "accountB", 50000l);
applicationContext.close();
}
}
Labels: Spring AOP, Spring3.0, Spring4