<aop:config>
<aop:aspect ref="loggingAspect">
<!-- all public methods with any 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.*.*(..))" id="logForAllMethods"/>
<!-- 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:after method="afterAdviceForAllMethods" pointcut-ref="logForAllMethods"/>
<aop:after method="afterAdviceForTransferMethods" pointcut-ref="logForAllTransfer"/>
</aop:aspect>
</aop:config>
<aop:pointcut expression="execution(* com.doj.aopapp.service.*.*(..))" id="logForAllMethods"/>
<aop:pointcut expression="execution(* com.doj.aopapp.service.*.transfer(*,*,*))" id="logForAllTransfer"/>
<properties>
<spring.version>4.3.7.RELEASE</spring.version>
<aspectj.version>1.8.9</aspectj.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<?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="loggingAspect"> <!-- all public methods with any 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.*.*(..))" id="logForAllMethods"/> <!-- 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:after method="afterAdviceForAllMethods" pointcut-ref="logForAllMethods"/> <aop:after method="afterAdviceForTransferMethods" pointcut-ref="logForAllTransfer"/> </aop:aspect> </aop:config> <bean id="transferService" class="com.doj.aopapp.service.TransferServiceImpl"/> <bean id="loggingAspect" class="com.doj.aopapp.aspect.LoggingAspect"/> </beans>
/**
*
*/
package com.doj.aopapp.service;
/**
* @author Dinesh.Rajput
*
*/
public interface TransferService {
void transfer(String accountA, String accountB, Long amount);
Double checkBalance(String account);
Long withdrawal(String account, Long amount);
void diposite(String account, Long amount);
}
/**
*
*/
package com.doj.aopapp.service;
/**
* @author Dinesh.Rajput
*
*/
public class TransferServiceImpl implements TransferService {
@Override
public void transfer(String accountA, String accountB, Long amount) {
System.out.println(amount+" Amount has been tranfered from "+accountA+" to "+accountB);
}
@Override
public Double checkBalance(String account) {
System.out.println("Available balance: 50000");
return new Double(50000);
}
@Override
public Long withdrawal(String account, Long amount) {
System.out.println("Withdrawal amount: " +amount);
return amount;
}
@Override
public void diposite(String account, Long amount) {
System.out.println(amount+" Amount has been diposited to "+account);
}
}
/**
*
*/
package com.doj.aopapp.aspect;
import org.aspectj.lang.JoinPoint;
/**
* @author Dinesh.Rajput
*
*/
public class LoggingAspect {
/**
* Declaring after advice
* @param jp
* @throws Throwable
*/
public void afterAdviceForAllMethods(JoinPoint jp) throws Throwable {
System.out.println("****LoggingAspect.afterAdviceForAllMethods() " + jp.getSignature().getName());
}
/**
* Declaring after 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
*/
public void afterAdviceForTransferMethods(JoinPoint jp) throws Throwable {
System.out.println("****LoggingAspect.afterAdviceForTransferMethods() " + jp.getSignature().getName());
}
}
/**
*
*/
package com.doj.aopapp.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.doj.aopapp.service.TransferService;
/**
* @author Dinesh.Rajput
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
TransferService transferService = applicationContext.getBean(TransferService.class);
transferService.transfer("accountA", "accountB", 50000l);
transferService.checkBalance("accountA");
transferService.diposite("accountA", 50000l);
transferService.withdrawal("accountB", 40000l);
applicationContext.close();
}
}

Labels: Spring AOP, Spring3.0, Spring4