Professional Documents
Culture Documents
Spring AOP
Spring AOP
—----------
In general, in enterprise applications, we will prepare the applications by
using Object orientation style, in Object Orientation style we will provide
the business logic and Services Logic[Authentication, Validations,
Internationalization, Logging,....] in a combined manner.
}
}
As per the Aspect Orientation, we have to separate all the services logic
from Business logic, we must declare each and every service as an aspect and
we must provide all the required aspects to the applications at runtime.
Aspect:
Aspect is a cross cutting concern or a service which we are going to apply in
the applications.
EX: Logging, Transactions, Security,.....
Advice:
Advice is the actual implementation of the Aspect , that is Aspect is a
concept And the Advice is the implementation of the Aspect.
EX: Logging is an aspect, but the same Logging has so many implementations
like Log4j, JAVA provided Logging,.... Each and every implementation of
Logging which we are going to use in our application is called an Advice.
JoinPoint:
JoinPoint is a point in the application execution where an aspect can be
applied.
Pointcut:
Pointcut is an expression, it represents which Aspects at which join point we
are going to apply in the application.
Target:
It is a Business component to apply all the aspects.
Proxy:
Proxy is an object which has the Target object with all the aspects or
Advices.
Introduction:
An introduction provides a very good environment to allow adding new methods
or attributes to the existing components without having changes in the code
and giving new behavior to the Component.
Advices in Spring:
—------------------
Advice is the real implementation of the Aspect, Advice is able to provide
the code for implementing Aspects.
EX: Logging is an aspect but it's one of the implementation Log4j is an
advice.
1. Before Advice
2. After Advice
3. After-Returning Advice
4. After-throwing Advice
5. Around Advice
Before Advice:
—---------------
Before Advice will have the Aspect implementation and it will be executed
just before the Business method execution.
EX:
Public class BeforeAdviceImpl implements MethodBeforeAdvice{
public void before(Method m, Object[] params, Object target){
—---Aspect Implementation—---
}
}
Where Object [First parameter] is representing the return value from the
Business method.
Where Object[] is representing parameters of the Business method.
Where Object [Third parameter] is representing Target object.
The above interface has the following method to execute after getting an
Exception.
}
AroundAdvice:
—------------
This advice will be executed before and after the business method execution.
Pointcut:
Pointcut defines at what joinpoint what advice/Advices has/have to be
applied.
1. Static Pointcut
2. Dynamic Pointcut
Static pointcut:
Static Pointcut defines the advice that is always executed.In Spring
applications we will define static pointcuts on the basis of the business
method and target class.
Spring framework has provided the following implementation classes for the
PointCut interface.
1. NameMatchMethodPointCut
2. Perl5RegexprMethodPointCut
3. JdkRegexMethodPointCut
—----
—----
Dynamic Pointcut:
DynamicPointcut is a pointcut, it will determine if the advice should be
executed or not on the basis of the runtime method arguments.
EX:
1. ControlFlowPointCut
2. DynamicMethodMatcherpointCut
<bean id=”pointcut”
class=”org.springframework.aop.support.NameMatchMethodPointcut”>
<property name=”mappedNames”>
<array>
<value>deposit</value>
<value>withdraw</value>
<value>transferFunds</value>
</array>
</property>
</bean>
<bean id=”pointcut”
class=”org.springframework.aop.support.Perl5RegexmethodPointcut”>
<property name=”pattern”>
<list>
<value>EmployeeService.*</value>
</list>
</property>
</bean>
<bean id=”advisor”
class=”org.springframework.aop.support.DefaultPointcutAdvisor”>
<property name=”pointcut” ref=”pointcut”/>
<property name=”advice” ref=”advice”/>
</bean>
EmployeeService.java
package com.durgasoft.service;
import com.durgasoft.beans.Employee;
EmployeeServiceImpl.java
package com.durgasoft.service;
import com.durgasoft.beans.Employee;
EmployeeValidatorAdvice.java
package com.durgasoft.advices;
import com.durgasoft.beans.Employee;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
System.out.println("================================================"
);
int eno = employee.getEno();
if(eno == 0){
System.out.println("Employee Number Is Required");
}else{
if(eno < 100 || eno > 999){
System.out.println("Employee Number Must be 3 digit
number");
}
}
String ename = employee.getEname();
if(ename == null || ename.equals("")){
System.out.println("Employee Name Is Required");
}
Main.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.service.EmployeeService;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
SpringConfig.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="employee" class="com.durgasoft.beans.Employee">
<property name="eno" value="123"/>
<property name="ename" value="Durga"/>
<property name="esal" value="50000"/>
<property name="eemail" value="durga@durgasoft.com"/>
<property name="emobile" value="91-9988776655"/>
</bean>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.durgasoft</groupId>
<artifactId>springaopapp01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.8</version>
</dependency>
</dependencies>
</project>
InstituteService.java
package com.durgasoft.service;
import com.durgasoft.beans.Student;
InstituteServiceImpl.java
package com.durgasoft.service;
import com.durgasoft.beans.Student;
@Override
public void courseEnquiry(Student student, String courseName) {
System.out.println("Student Course Enquiry Details");
System.out.println("---------------------------------");
System.out.println("Student Name :
"+student.getSname());
System.out.println("Student Qualification :
"+student.getSqual());
System.out.println("Student Email Id :
"+student.getSemail());
System.out.println("Student Mobile Number :
"+student.getSmobile());
System.out.println("Student Address :
"+student.getSaddr());
System.out.println("Student Course Name : "+courseName);
}
@Override
public void courseRegistration(Student student, String courseName)
{
System.out.println("Student Course Registration Details");
System.out.println("---------------------------------");
System.out.println("Student Name :
"+student.getSname());
System.out.println("Student Qualification :
"+student.getSqual());
System.out.println("Student Email Id :
"+student.getSemail());
System.out.println("Student Mobile Number :
"+student.getSmobile());
System.out.println("Student Address :
"+student.getSaddr());
System.out.println("Student Course Name : "+courseName);
}
}
ThanQAdvice.java
package com.durgasoft.advices;
import com.durgasoft.beans.Student;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
Main.java
package com.durgasoft;
import com.durgasoft.beans.Student;
import com.durgasoft.service.InstituteService;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
System.out.println();
instituteService.courseRegistration(student, "JAVA");
}
}
SpringConfig.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="student" class="com.durgasoft.beans.Student">
<property name="sname" value="Durga"/>
<property name="squal" value="MTech"/>
<property name="semail" value="durga@durgasoft.com"/>
<property name="smobile" value="91-9988776655"/>
<property name="saddr" value="Hyd"/>
</bean>
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.durgasoft</groupId>
<artifactId>springaopapp02</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.8</version>
</dependency>
</dependencies>
</project>
MovieService.java
package com.durgasoft.service;
import com.durgasoft.beans.Movie;
MovieServiceImpl.java
package com.durgasoft.beans;
import com.durgasoft.service.MovieService;
@Override
public void playMovie(Movie movie) throws Exception {
System.out.println("Movie "+movie.getMovieName()+" Started at
"+movie.getShowTime());
throw new RuntimeException("Power Failure Occurred");
}
}
MoneyReturnAdvice.java
package com.durgasoft.advices;
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;
Main.java
package com.durgasoft;
import com.durgasoft.beans.Movie;
import com.durgasoft.service.MovieService;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
}
}
}
SpringConfig.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- Movie -->
<bean id="movie" class="com.durgasoft.beans.Movie">
<property name="movieName" value="Bahubali"/>
<property name="showTime" value="9:00PM"/>
<property name="price" value="500"/>
</bean>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.durgasoft</groupId>
<artifactId>springaopapp03</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.8</version>
</dependency>
</dependencies>
</project>
Example on AroundAdvice:
Account.java
package com.durgasoft.beans;
Cheque.java
package com.durgasoft.beans;
TransactionService.java
package com.durgasoft.service;
import com.durgasoft.beans.Account;
import com.durgasoft.beans.Cheque;
TransactionServiceImpl.java
package com.durgasoft.service;
import com.durgasoft.beans.Account;
import com.durgasoft.beans.Cheque;
public class TransactionServiceImpl implements TransactionService{
@Override
public void debit(Account account, Cheque cheque) {
System.out.println("**********Transaction START*********");
int initialAmount = account.getBalance();
int debitAmount = cheque.getAmount();
int totalAmount = initialAmount - debitAmount;
account.setBalance(totalAmount);
System.out.println("********Transaction Success*********");
System.out.println("********Amount is debited from
Account*******");
System.out.println("********Transaction END*********");
}
}
ChequeClearenceAdvice.java
package com.durgasoft.advices;
import com.durgasoft.beans.Account;
import com.durgasoft.beans.Cheque;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
Main.java
package com.durgasoft;
import com.durgasoft.beans.Account;
import com.durgasoft.beans.Cheque;
import com.durgasoft.service.TransactionService;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
}
}
SpringConfig.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- Beans -->
<bean id="account" class="com.durgasoft.beans.Account">
<property name="accNo" value="abc123"/>
<property name="accHolderName" value="Durga"/>
<property name="accType" value="Savings"/>
<property name="balance" value="50000"/>
</bean>
<bean id="cheque" class="com.durgasoft.beans.Cheque">
<property name="chequeNumber" value="1234556789"/>
<property name="amount" value="10000"/>
</bean>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.durgasoft</groupId>
<artifactId>springaopapp04</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.8</version>
</dependency>
</dependencies>
</project>
AspectJ:
—--------
AspectJ is a language, it is able to represent AOP and it has provided
specialized syntaxes to express concerns.
1. Declarative Approach.
2. @Aspectj annotation style approach.
1. Declarative Approach:
—------------------------
IN declarative approach, we will use a set AOP tags which are provided by
the AOP namespace in the Spring COnfiguration file in order to declare
Aspects, Advices, Pointcuts,.....
SpringConfig.xml
—----------------
<beans ………..
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
—-----
<aop:config>
—-----
</aop:config>
—---
</beans>
Where <aop:config> tag will have all the aspects configuration and all the
specific business methods related configurations like
advice,pointcuts,....
1. Declaring Aspects:
To declare Aspects by using AOP tags we have to use the following tags in
the Spring Configuration file.
<beans …… >
—--
<aop:config>
<aop:aspect id=”--” ref=”---”/>
—---
</aop:config>
—--
</beans>
EX:
<beans>
<bean id=”loggingBean”
class=”com.durgasoft.aspects.EmployeeLoggingBean”/>
<aop:config>
<aop:aspect id=”loggingAspect” ref=”loggingBean”>
—---
</aop:aspect>
</aop:config>
</beans>
2.Declaring Pointcuts:
A pointcut helps in determining the join points to be executed with
different advices.
<beans…..>
<aop:config>
<aop:aspect id=”--” class=”---”>
<aop:pointcut id=”--” expression=”---”/>
—----
</aop:aspect>
</aop:config>
</beans>
EX:
<beans>
<bean id=”loggingBean”
class=”com.durgasoft.aspects.EmployeeLoggingBean”/>
<aop:config>
<aop:aspect id=”loggingAspect” ref=”loggingBean”>
<aop:pointcut id=”logPointcut”
expression=”execution(*
com.durgasoft.service.EmployeeService.*(...))”
/>
</aop:aspect>
</aop:config>
</beans>
2. execution(* EmployeeService.*(..))
This expression is able to represent all the business methods which are
available in EmployeeService in the current package and having any number
of parameters and having any access modifier and any Return type.
3. execution(public * com.durgasoft.service.EmployeeService.*(..)):
This expression is able to represent all the business methods which are
available in EmployeeService , which are declared with public and which
are having any return type and which are having any number of parameters.
4. execution(public Employee com.durgasoft.service.EmployeeService.*(..)):
This expression is able to represent all the business methods which are
having public access modifiers , which are having only Employee return
type and which are having any number of parameters.
5. execution(public Employee
com.durgasoft.service.EmployeeService.*(Employee ,..)):
This expression is able to represent all the business methods of
EmployeeService which are having public access modifiers, which are having
Employee return type and which are having the first parameter is Employee.
3. Declaring Advices:
—--------------------
Spring AspectJ is providing the following five advices.
1. <aop:before>
2. <aop:after>
3. <aop:after-returning>
4. <aop:around>
5. <aop:after-throwing>
In all the above advices, there are two attributes.
1. Method: it will take an advice method name.
2. pointcut-ref: it will take pointcut reference value.
EX:
<beans>
<bean id=”loggingBean”
class=”com.durgasoft.aspects.EmployeeLoggingBean”/>
<aop:config>
<aop:aspect id=”loggingAspect” ref=”loggingBean”>
<aop:pointcut id=”logPointcut”
expression=”execution(*
com.durgasoft.service.EmployeeService.*(...))”
/>
<aop:before method=”logBefore”
pointcut-ref=”logPointcut”/>
<aop:after method=”logAfter”
pointcut-ref=”logPointcut”/>
—--
</aop:aspect>
</aop:config>
</beans>
EX:
—--
Employee.java
package com.durgasoft.beans;
EmployeeService.java
package com.durgasoft.service;
import com.durgasoft.beans.Employee;
EmployeeServiceImpl.java
package com.durgasoft.service;
import com.durgasoft.beans.Employee;
@Override
public String createEmployee(Employee employee) throws Exception {
System.out.println("Employee "+employee.getEno()+" Inserted
Successfullt from createEmployee() method");
return "SUCCESS";
}
@Override
public Employee searchEmployee(int eno) {
System.out.println("Employee "+eno+" found from
searchEmployee() method");
return null;
}
@Override
public String updateEmployee(Employee employee) {
System.out.println("Employee "+employee.getEno()+" Updated
Successfully from updateEmployee() method");
return "SUCCESS";
}
@Override
public String deleteEmployee(Employee employee) {
System.out.println("Employee "+employee.getEno()+" Deleted
Successfully");
return "SUCCESS";
}
}
LoggingAspect.java
package com.durgasoft.aspects;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
Main.java
package com.durgasoft;
import com.durgasoft.beans.Employee;
import com.durgasoft.service.EmployeeService;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
}
System.out.println(message);
*/
//employeeService.searchEmployee(111);
//employeeService.updateEmployee(employee);
}
}
SpringConfig.xml
<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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="employee" class="com.durgasoft.beans.Employee">
<property name="eno" value="111"/>
<property name="ename" value="Durga"/>
<property name="esal" value="50000"/>
<property name="eaddr" value="Hyd"/>
</bean>
<!-- Target -->
<bean id="employeeService"
class="com.durgasoft.service.EmployeeServiceImpl"/>
<aop:config>
<aop:aspect id="loggingAspect" ref="loggingAspectBean">
<aop:pointcut id="employeePointcut"
expression="execution(*
com.durgasoft.service.EmployeeService.*(..))"/>
<aop:before method="before"
pointcut-ref="employeePointcut"/>
<aop:after method="after"
pointcut-ref="employeePointcut"/>
<aop:after-returning method="afterReturning"
pointcut-ref="employeePointcut" returning="result"/>
<aop:around method="around"
pointcut-ref="employeePointcut"/>
<aop:after-throwing method="afterThrowing"
pointcut-ref="employeePointcut" throwing="throwable"/>
</aop:aspect>
</aop:config>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.durgasoft</groupId>
<artifactId>springaopapp05</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.9</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectj-tools</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.19</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.19</version>
</dependency>
</dependencies>
</project>
1. @Aspect
2. @Pointcut
3. @Before
4. @After
5. @AfterReturning
6. @Around
7. @AfterThrowing
<aop:aspectj-autoproxy/>
EX:
Account.java
package com.durgasoft.beans;
TransactionService.java
package com.durgasoft.service;
import com.durgasoft.beans.Account;
import com.durgasoft.exceptions.InsufficientFundsException;
TransactionServiceImpl.java
package com.durgasoft.service;
import com.durgasoft.beans.Account;
import com.durgasoft.exceptions.InsufficientFundsException;
@Override
public String withdraw(Account account, int withdrawAmount) throws
InsufficientFundsException {
System.out.println("from withdraw() method , Transaction
Started");
String status = "";
if(account.getBalance() > withdrawAmount){
double totalbalance = account.getBalance() -
withdrawAmount;
account.setBalance(totalbalance);
System.out.println("From withdraw() method, transaction
Completed");
status = "SUCCESS";
}else{
status = "FAILURE";
throw new InsufficientFundsException("Funds Are Not
Sufficient in Your Account");
}
return status;
}
}
InsufficientFundsException.java
package com.durgasoft.exceptions;
TransactionAspect.java
package com.durgasoft.aspects;
import com.durgasoft.beans.Account;
import com.durgasoft.exceptions.InsufficientFundsException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect
public class TransactionAspect {
@Before("execution(*
com.durgasoft.service.TransactionService.*(..))")
public void before(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
Account account = (Account) args[0];
System.out.println("Before Advice : Initial Balance :
"+account.getBalance());
}
@After("execution(*
com.durgasoft.service.TransactionService.*(..))")
public void after(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
Account account = (Account) args[0];
System.out.println("After Advice : Total Balance :
"+account.getBalance());
}
@AfterReturning(pointcut = "execution(*
com.durgasoft.service.TransactionService.*(..))", returning =
"results")
public void afterReturning(JoinPoint joinPoint, String results){
Object[] args = joinPoint.getArgs();
Account account = (Account) args[0];
System.out.println("After Returning Advice : Transaction
Status : "+results);
}
@Around("execution(*
com.durgasoft.service.TransactionService.*(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) {
System.out.println("Around Advice : Before
"+proceedingJoinPoint.getSignature().getName()+" Method Execution");
String status = "";
try {
status = (String) proceedingJoinPoint.proceed();
} catch (Throwable e) {
}
System.out.println("Around Advice : After : Transaction Status
: "+status);
}
@AfterThrowing(pointcut = "execution(*
com.durgasoft.service.TransactionService.*(..))", throwing =
"exception")
public void afterThrowing(JoinPoint joinPoint,
InsufficientFundsException exception){
Main.java
package com.durgasoft;
import com.durgasoft.beans.Account;
import com.durgasoft.exceptions.InsufficientFundsException;
import com.durgasoft.service.TransactionService;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
}
}
}
SpringConfig.xml
<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:aspectj-autoproxy/>
<bean id="account" class="com.durgasoft.beans.Account">
<property name="accNo" value="abc123"/>
<property name="accHolderName" value="Durga"/>
<property name="accType" value="Savings"/>
<property name="balance" value="25000"/>
</bean>
<bean id="transactionService"
class="com.durgasoft.service.TransactionServiceImpl"/>
<bean id="transactionAspect"
class="com.durgasoft.aspects.TransactionAspect"/>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.durgasoft</groupId>
<artifactId>springaopapp06</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.9</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectj-tools</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.19</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.19</version>
</dependency>
</dependencies>
</project>
======================================================