Download as pdf or txt
Download as pdf or txt
You are on page 1of 79

Advanced Java

Anytime, anywhere

-Professor Shweta Waghmare


Module 4 : Spring and AOP
Topics in Module 4

❏ AspectOriented Programming with Spring (last year question),


❏ Types of advices (imp),
❏ Defining PointCut Designator and Expression (imp: Explain PointCut Designator by
annotation example),
❏ Annotations. (@Aspect,@Before,@After…)
❏ Self learning topics AspectJ()
AOP

Class ⇒ object
(define) (key unit data represent class)

Aspect : Specialized Class that


addresses one of the “cross cutting
concern”

Non-functional requirement are

“cross cutting concern” ⇒


AOP
● One of the key components of Spring Framework is the Aspect oriented
programming (AOP) framework.
● Aspect-Oriented Programming entails breaking down program logic into distinct
parts called so-called concerns.
● The functions that span multiple points of an application are called cross-cutting
concerns and these cross-cutting concerns are conceptually separate from the
application's business logic.

● There are various common good examples of aspects like logging, auditing,
declarative transactions, security, caching, etc.
AOP
● The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity
is the aspect.
● Dependency Injection helps you decouple your application objects from each other and
AOP helps you decouple cross-cutting concerns from the objects that they affect. AOP is
like triggers.

● Spring AOP module provides interceptors to intercept an application. For example, when
a method is executed, you can add extra functionality before or after the method
execution.
● AOP the unit of modularity is the aspect.
JUST FOR REFERENCE :
Modular programming is the process of subdividing a computer program into separate
sub-programs. A module is a separate software component. It can often be used in a variety of
applications and functions with other components of the system.
● Some programs might have thousands or millions of lines and to manage such programs it
becomes quite difficult as there might be too many of syntax errors or logical errors present
in the program, so to manage such type of programs concept of modular programming
approached.
Need of spring AOP
The Spring AOP provides the pluggable way of dynamically adding the additional
concern before, after or around the actual logic. Suppose there are 10 methods in a
class as given below:
The Scenario: You have to maintain the log and send the notification after calling
methods starting from m.

A problem without using the AOP: You can call methods which maintain logs and
sends the notification from the methods starting with m. For that, you need to write
code for all the 5 methods.

The solution with Aspect Oriented Programming: You don’t have to call methods
from the method. You can define additional concerns like maintaining a log,
sending notification etc. as a method of a class. Its entry is given in the xml file.
In future, if client says to remove the notifier functionality, we need to
change only in the xml file. So, maintenance is easy in AOP.
Where to use Spring Aspect Oriented Programming

Some of the cases where AOP is frequently used:


● To provide declarative enterprise services. For example, as declarative
transaction management.
● It allows users for implementing custom aspects.
AOP TERMINOLOGY
What is advice, joinpoint or pointcut?
Aspect : This is a module which has a set of APIs providing cross-cutting requirements.
● For example, a logging module would be called AOP aspect for logging. An application can
have any number of aspects depending on the requirement.
It can be normal class configured through XML configuration or through regular
classes annotated with @Aspect.

Join point : This represents a point in your application where you can plug-in the AOP
aspect. You can also say, it is the actual place in the application where an action will be taken
using Spring AOP framework.
● An application has thousands of opportunities or points to apply Advice. These points are
known as join points. For example – Advice can be applied at every invocation of a
method or exception be thrown or at various other points. But Spring AOP currently
supports only method execution join points (advising the execution of methods on Spring
beans).
Join point is any point in your program such as method execution, exception handling,
field access etc. Spring supports only method execution join points
Advice :
Advice represents an action taken by an aspect at a particular join point. There
are different types of advices:

● Before Advice: it executes before a join point.

● After Returning Advice: it executes after a joint point completes normally.

● After Throwing Advice: it executes if the method exits by throwing an


exception.

● After (finally) Advice: it executes after a join point regardless of join point exit
whether normally or exceptional return.

● Around Advice: It executes before and after a join point


Pointcut : This is a set of one or more join points where an advice should be
executed. You can specify pointcuts using expressions or patterns.
Introduction : An introduction allows you to add new methods or attributes to the
existing classes.
Target object (java Class): The object being advised by one or more aspects
(class). This object will always be a proxied object, also referred to as the advised
object.
Weaving : Weaving is the process of linking aspects with other application types or
objects to create an advised object. This can be done at compile time, load time, or
at runtime.Spring AOP performs weaving at runtime.
AOP Proxy : AOP Proxy use for implementing the aspect contracts which create by
AOP Framework. It will be a JDK dynamic proxy in Spring Framework.
Interceptor : It is an aspect which contains advice, joinpoints etc.
What is advice, joinpoint or pointcut?

1. An important term in AOP is advice. It is the action taken by an aspect at a


particular join-point.

2. Joinpoint is a point of execution of the program, such as the execution of a method


or the handling of an exception. In Spring AOP, a joinpoint always represents a
method execution.

3. Pointcut is a predicate or expression that matches join points.(constraints)

4. Advice is associated with a pointcut expression and runs at any join point
matched by the pointcut.

5. Spring uses the AspectJ(framework) pointcut expression language by default.


What is advice, joinpoint or pointcut?
TYPES OF ADVICE
TYPES OF ADVICE

Before: Run the advice before the execution of the method.


After: Run the advice after the execution of a method regardless of the outcome.
After-returning: Run the advice after the execution of the method only after the
successful completion of the method.(returning value)
After-throwing: Run the advice after execution of method only if the method
exits throwing an exception.
Around: Run the advice before and after advice method gets invoked.
Dominant Frameworks in AOP

AOP includes programming methods and frameworks on which modularisation of code is


supported and implemented. Let’s have a look at the three dominant frameworks in AOP:

● AspectJ: It is an extension for Java programming created at PARC research centre. It


uses Java like syntax and included IDE integrations for displaying crosscutting structure.
It has its own compiler and weaver, on using it enables the use of full AspectJ language.
● JBoss: It is an open source Java application server developed by JBoss, used for Java
development.
● Spring: It uses XML based configuration for implementing AOP, also it uses annotations
which are interpreted by using a library supplied by AspectJ for parsing and matching.
Currently, AspectJ libraries with Spring framework are dominant in the market
Custom Aspects Implementation
Spring supports the @AspectJ annotation style approach and the schema-based approach to
implement custom aspects.

1. XML Schema based : Aspects are implemented using the regular classes along with
XML based configuration.

2. @AspectJ based : @AspectJ refers to a style of declaring aspects as regular Java classes
annotated with Java 5 annotations.
1. XML Schema Based AOP with Spring
To use the AOP namespace tags described in this section, you need to import the
springAOP schema as described −

<?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"
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 ">
<!-- bean definition & AOP specific configuration -->
</beans>
You will also need the following AspectJ libraries on the CLASSPATH of your application.
These libraries are available in the 'lib' directory of an AspectJ installation, otherwise
you can download them from the internet.
● aspectjrt.jar
● aspectjweaver.jar
● aspectj.jar
● aopalliance.jar
Declaring an aspect
An aspect is declared using the <aop:aspect> element, and the backing bean is referenced using
the ref attribute as follows −
<aop:config>
<aop:aspect id = "myAspect" ref = "aBean">
...
</aop:aspect>
</aop:config>
<bean id = "aBean" class = "...">
...
</bean>

Here "aBean" will be configured and dependency injected just like any other Spring bean as you
have seen in the previous chapters.
Note :Aspect: An aspect is a class that implements enterprise application concerns that cut across
multiple classes, such as transaction management. Aspects can be a normal class configured through Spring
XML configuration or we can use Spring AspectJ integration to define a class as Aspect using @Aspect
annotation.
Declaring a pointcut
A pointcut helps in determining the join points (i.e methods) of interest to be executed
with different advices. While working with XML Schema-based configuration, pointcut will be
defined as follows −
<aop:config>
<aop:aspect id = "myAspect" ref = "aBean">
<aop:pointcut id = "businessService"
expression = "execution(* com.xyz.myapp.service.*.*(..))"/>
...
</aop:aspect>
</aop:config>
<bean id = "aBean" class = "...">
...

</bean>
Note : Joinpoint is a point of execution of the program, such as the execution of a method or
the handling of an exception. In Spring AOP, a joinpoint always represents a method execution.
Pointcut is a predicate or expression that matches join points.
Declaring pointcut example
The following example defines a pointcut named 'businessService' that will match the execution of
getName() method available in the Student class under the package com.tutorialspoint −
Declaring Advices
You can declare any of the five
advices inside an <aop:aspect>
using the <aop:{ADVICE NAME}>

element as given below:


Complete EXAMPLE :

1 Create a project with a name SpringExample and create a package under the src
folder in the created project.

2 Add required Spring libraries using Add External JARs option.


3 Add Spring AOP specific libraries aspectjrt.jar, asm.jar, cglib.jar, aspectjweaver.jar
and aspectj.jar in the project.
4 Create Java classes Logging(aspect), Student(regular java) and MainApp(test file)
under the package.
5 Create Beans configuration file Beans.xml under the src folder.
6 The final step is to create the content of all the Java files and Bean Configuration file
and run the application.
Logging.java file.(act as Aspect using xml based approach)
package com.tutorialspoint;
public class Logging {
//* This is the method which I would like to execute before a selected method execution.
public void beforeAdvice(){
System.out.println("Going to setup student profile.");
}
//after a selected method execution.
public void afterAdvice(){
System.out.println("Student profile has been setup.");
}
// when any method returns.
public void afterReturningAdvice(Object retVal) {
System.out.println("Returning:" + retVal.toString() );
}
//if there is an exception raised.
public void afterThrowingAdvice(IllegalArgumentException ex){
System.out.println("There has been an exception: " + ex.toString());
}
}
Student.java file(regular)
package com.tutorialspoint;
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
System.out.println("Age : " + age );
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
System.out.println("Name : " + name );
return name;
}
public void printThrowException(){
System.out.println("Exception raised");
throw new IllegalArgumentException();
} }
MainApp.java file(test file)
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Student student = (Student) context.getBean("student");
student.getName();
student.getAge();
student.printThrowException();
}
}
Beans.xml
<?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"
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 ">
Once you are done creating the source and bean configuration files, let us run the
application. If everything is fine with your application, it will print the following message −
Pointcut expression (imp)
How to match method signature patterns?
The most typical pointcut expressions are used to match a number of methods by their
signatures.
1. Match all methods within a class in another package

For example, the following pointcut expression matches all of the methods declared in
the EmployeeManager class. The preceding wildcard matches methods with any
modifier (public, protected, and private) and any return type. The two dots in the
argument list match any number of arguments.

execution(* com.howtodoinjava.EmployeeManager.*(..))
2.Match all methods within a class within same package

You can omit the package name if the target class or interface is located in the
same package as this aspect.

execution(* EmployeeManager.*(..))

3.Match all public methods in EmployeeManager

Use public keyword in start, and use * to match any return type(void,int,float,string).

execution(public * EmployeeManager.*(..))
4.Match all public methods in EmployeeManager with return type int

Use public keyword and return type in start.

execution(public int EmployeeManager.*(..))

5.Match all public methods in EmployeeManager with return type int and first
parameter as String

Use public keyword and return type in start. Also, specify your first parameter as well. Rest
parameters can be matched through two dots.

Execution(public int EmployeeManager.*(String , ..))


2. @AspectJ Based AOP with Spring
@AspectJ refers to a style of declaring aspects as regular Java classes annotated with Java 5
annotations. The @AspectJ support is enabled by including the following element inside your
XML Schema-based configuration file.
<aop:aspectj-autoproxy/>

You will also need the following AspectJ libraries on the classpath of your application.
These libraries are available in the 'lib' directory of an AspectJ installation, otherwise you
can download them from the internet.
● aspectjrt.jar
● aspectjweaver.jar
● aspectj.jar
● aopalliance.jar
Declaring an aspect

Aspects classes are like any other normal bean and may have methods and fields just
like any other class, except that they will be annotated with @Aspect as follows −
package org.xyz;
import org.aspectj.lang.annotation.Aspect;
@Aspect //annotation
public class AspectModule {
}
They will be configured in XML like any other bean as follows −
<bean id = "myAspect" class = "org.xyz.AspectModule">
<!-- configure properties of aspect here as normal -->
</bean>
Declaring a pointcut
A pointcut helps in determining the join points (ie methods) of interest to be executed
with different advices. While working with @AspectJ-based configuration, pointcut
declaration has two parts −

● A pointcut expression that determines exactly which method executions we


are interested in.

● A pointcut signature comprising a name and any number of parameters. The


actual body of the method is irrelevant and in fact should be empty.
The following example defines a pointcut named 'businessService' that will match the
execution of every method available in the classes under the package
com.xyz.myapp.service −
import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.xyz.myapp.service.*.*(..))") //expression
private void businessService() {} // signature or pointcut id

The following example defines a pointcut named 'getname' that will match the execution of
getName() method available in the Student class under the package com.tutorialspoint −
import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.tutorialspoint.Student.getName(..))")
private void getname() {}
Declaring advices

You can declare any of the five advices using @{ADVICE-NAME} annotations as given in the code
snippet. This assumes that you already have defined a pointcut signature method businessService()

@Before("businessService()") //<aop:before pointcut-ref=”buisnessService” method=”doBeforetask”/>


public void doBeforeTask(){ … }

@After("businessService()")
public void doAfterTask(){ … }

@AfterReturning(pointcut = "businessService()", returning = "retVal")


public void doAfterReturnningTask(Object retVal) { // you can intercept retVal here. … }

@AfterThrowing(pointcut = "businessService()", throwing = "ex")


public void doAfterThrowingTask(Exception ex) {
// you can intercept thrown exception here. … }

@Around("businessService()")
public void doAroundTask(){ … }
You can define a pointcut inline for any of the advices. Following is an example to define
inline pointcut for before advice −

@Before("execution(* com.xyz.myapp.service.*.*(..))")

public doBeforeTask(){

…}
@AspectJ Based AOP Complete Example :
package com.tutorialspoint;
@Aspect
public class Logging {
@Pointcut("execution(* com.tutorialspoint.*.*(..))")
private void selectAll(){}
@Before("selectAll()") Logging.java file

public void beforeAdvice(){


System.out.println("Going to setup student profile."); }
@After("selectAll()")
public void afterAdvice(){
System.out.println("Student profile has been setup."); }
@AfterReturning(pointcut = "selectAll()", returning = "abc")
public void afterReturningAdvice(Object abc){
System.out.println("Returning:" + retVal.toString() ); }
@AfterThrowing(pointcut = "selectAll()", throwing = "as")
public void AfterThrowingAdvice(IllegalArgumentException as){
System.out.println("There has been an exception: " + ex.toString()); }
}
Annotations(AOP AspectJ)
Spring AspectJ AOP implementation provides many annotations:

1. @Aspect declares the class as aspect.

2. @Pointcut declares the pointcut expression.


Annotations(AOP AspectJ)
The annotations used to create advices are given below:

1. @Before declares the before advice. It is applied before calling the actual method.

2. @After declares the after advice. It is applied after calling the actual method and before returning

result.

3. @AfterReturning declares the after returning advice. It is applied after calling the actual method

and before returning result. But you can get the result value in the advice.

4. @Around declares the around advice. It is applied before and after calling the actual method.

5. @AfterThrowing declares the throws advice. It is applied if actual method throws exception.
Understanding Pointcut

Pointcut is an expression language of Spring AOP.

The @Pointcut annotation is used to define the pointcut. We can refer the pointcut
expression by name also. Let's see the simple example of pointcut expression.

@Pointcut("execution(* Operation.*(..))") (Operation=Classname)


private void doSomething() {}
The name of the pointcut expression is doSomething().

It will be applied on all the methods of Operation class regardless of return type.
Understanding Pointcut Expressions
Let's try the understand the pointcut expressions by the examples given below:

● @Pointcut("execution(public * *(..))") : It will be applied on all the public methods.


● @Pointcut("execution(public Operation.*(..))") : It will be applied on all the public methods
of Operation class.
● @Pointcut("execution(* Operation.*(..))") : It will be applied on all the methods of
Operation class.
● @Pointcut("execution(public Employee.set*(..))") : It will be applied on all the public
setter methods of Employee class.
● @Pointcut("execution(int Operation.*(..))") : It will be applied on all the methods of
Operation class that returns int value.
Declaring a Pointcut (imp: Explain PointCut Designator by annotation example)
Declaring a pointcut is simple, all you need is annotate the method with
@Pointcut(POINTCUT_EXPRESSION).
@Pointcut("execution(* transfer(..))") // the pointcut expression
private void anyTransfer() {} //pointcut signature
It is optional to use @Pointcut instead, the pointcut expressions can be directly used inside
the advice annotations as shown below.
● Before advice –@Before("execution(* transfer(..))")
● Around advice – @Around
● After returning – @AfterReturning
● After throwing – @AfterThrowing
● After (finally) advice – @After
@Before("execution(* c.jbd.saop.gettingstarted.dao.*.add(..))")
public void allRepoAddMethods(JoinPoint joinPoint) {
//Aspect body
}
The term execution inside the @Before or @Pointcut annotation is known as Pointcut
designators. Spring provides several designators
PointCut Designators
A pointcut expression starts with a pointcut designator (PCD), which is a keyword telling
Spring AOP what to match. There are several pointcut designators, such as the execution
of a method, a type, method arguments, or annotations.

execution //Designator name


"target(* transfer(..))"
@target
Class transfer
@target
Class transfer1
execution

The primary Spring PCD is execution, which matches method execution join points:

@Pointcut("execution(public String
com.baeldung.pointcutadvice.dao.FooDao.findById(Long))")

This example pointcut will exactly match the execution of the findById method of the FooDao
class. This works, but it's not very flexible. Suppose we'd like to match all the methods of the
FooDao class, which may have different signatures, return types, and arguments. To achieve
this, we can use wildcards:

@Pointcut("execution(*
com.baeldung.pointcutadvice.dao.FooDao.*(..))")

Here, the first wildcard matches any return value, the second matches any method name, and
the (..) pattern matches any number of parameters (zero or more).
within

Another way to achieve the same result as the previous section is by using the within
PCD, which limits matching to join points of certain types:

@Pointcut("within(com.baeldung.pointcutadvice.dao.FooDao)")

We can also match any type within the com.baeldung package or a sub-package:

@Pointcut("within(com.baeldung..*)")
this and target
this limits matching to join points where the bean reference is an instance of the given type, while
target limits matching to join points where the target object is an instance of the given type. The
former works when Spring AOP creates a CGLIB-based proxy, and the latter is used when a JDK-based
proxy is created. Suppose that the target class implements an interface:

public class FooDao implements BarDao { ...}

In this case, Spring AOP will use the JDK-based proxy, and we should use the target PCD because the
proxied object will be an instance of the Proxy class and implement the BarDao interface:

@Pointcut("target(com.baeldung.pointcutadvice.dao.BarDao)")

On the other hand, if FooDao doesn't implement any interface, or the proxyTargetClass property is set to
true, then the proxied object will be a subclass of FooDao and we can use the this PCD:

@Pointcut("this(com.baeldung.pointcutadvice.dao.FooDao)")
args

We can use this PCD for matching particular method arguments:

@Pointcut("execution(* *..find*(Long))")

This pointcut matches any method that starts with find and has only one parameter
of type Long. If we want to match a method with any number of parameters, but still
having the fist parameter of type Long, we can use the following expression:

@Pointcut("execution(* *..find*(Long,..))")
@target

The @target PCD (not to be confused with the target PCD described above) limits
matching to join points where the class of the executing object has an annotation of
the given type:

@Pointcut("@target(org.springframework.stereotype.Repository)
")
@within

This PCD limits matching to join points within types that have the given annotation:

@Pointcut("@within(org.springframework.stereotype.Repository)")

Which is equivalent to:

@Pointcut("within(@org.springframework.stereotype.Repository
*)")
Supported Pointcut Designators

Spring AOP supports the following Pointcut Designators (PCD).

● execution – for matching method execution join points. This is the most widely used
PCD.//d1

● within – for matching methods of classes within certain types e.g. classes within a
package. //2
● @within – for matching to join points within types (target object class) that have the
given annotation.//a
● this – for matching to join points (the execution of methods) where the bean reference
(Spring AOP proxy) is an instance of the given type.//d3
● target – for matching with the target object of the specific instance type.//d4
● @target – for matching with the target object annotated with a specific annotation.
Supported Pointcut Designators

● args – for matching with methods where its arguments are of a specific type.//d5
● @args – for matching with methods where its arguments are annotated with a specific
annotation.
● @annotation (custom annotation concept)– for matching to join points where the subject
(method) of the Joinpoint has the given annotation.
● bean (idOrNameOfBean) – This PCD lets you limit the matching of join points to a particular
named Spring bean or to a set of named Spring beans (when using wildcards).//d6

● @Pointcut("bean(student)(* Employee.*(..))")

AspectJ provides several other designators which are not supported in Spring AOP, only the above
mentioned PCD are supported.
Combining Pointcut Expressions

You can combine multiple pointcut expressions by using AND – &&, OR – || and NOT – !. You can also
refer to pointcut expressions by name. The following example shows the same:

@Pointcut("execution(public * *(..))")
private void anyPublicOperation() {} //p1

@Pointcut("within(com.jsbd.trading..*)")
private void inTrading() {} //p2

@Pointcut("anyPublicOperation() && inTrading()")


private void tradingOperation() {} //p3=new name for p2+p1
Various Pointcut designators example
a. execution designator
This is the most widely used designator that everyone needs to know, used to match the method execution
Joinpoint. The syntax of an execution() designator is discussed below.

execution(modifiers-pattern? return-type-pattern declaring-type-pattern?


name-pattern( param-pattern) //avoid

throws-pattern?)

● ? – Means optional to specify, e.g. modifiers, throws_exception(1.modifier)


● * – A star in the pattern represents a wildcard(* =all type of modifier/returntype)
● () – For parameter_pattern means no arguments
● (..) – For matching for parameter_pattern with zero or more arguments of any type
● (*) – pattern matches a method that takes one parameter of any type.
● (*, String) – matches a method that takes two parameters, first one is of any type and the second
parameter of type string
b. within and @within designators
The within is used for matching methods of classes within certain types e.g. classes within a package.

Similarly, the @within is used for matching with join points (method execution) where the declared class of
the target object has a given annotation.
c. The this designator
It limits the matching to join points (the execution of methods) where the bean reference
(Spring AOP proxy) is an instance of the given type.

For example, match with any join point (method execution) where the proxy implements the
AccountService interface.

"this(com.jbd.service.AccountService)"
d. The target and @target designators
The target designator limits matching to join points (the execution of methods) where the target
object (application object being proxied) is an instance of the given type. e.g. when the target
object implements a specific interface.

Example, any join point where the target object implements the AccountService interface.

"target(com.jbd.service.AccountService)"
Similarly, the @target is used to Limit matching to join points (the execution of methods) where the
class of the executing object has an annotation of the given type.

Example, any join point where the target object has a @Transactional annotation.

"@target(org.springframework.transaction.annotation.Transactional)"
Spring AOP - Proxy

So far, we've declared aspects using <aop:config> or < aop:aspectj-autoproxy>. We can create a
proxy programmatically as well as invoke the aspects programmatically using the proxy object.

SYNTAX:
Where,

● AspectJProxyFactory − Factory class to create a proxy object.


● Logging.class − Class of the Aspect containing advices.
● Student − Business class to be advised.(regular java class where getter and
setter are there)

1. Update the project Student (name of project just an example)


2. Update the bean configuration and run the application
Explain PointCut
Designator by
annotation example

PointCut annotation

Before annotation
OUTPUT : ?
❏ Annotations. (@Aspect,@Before,@After…)
❏ Ref : 1. https://www.javatpoint.com/spring-aop-aspectj-annotation-example

Official site :
https://docs.spring.io/spring-framework/docs/4.3.15.RELEASE/spring-framework-reference/
html/aop.html

https://www.baeldung.com/spring-aop-pointcut-tutorial#:~:text=Pointcut%20Designators,%
2C%20method%20arguments%2C%20or%20annotations.

You might also like