Advanced Java (Module 4)
Advanced Java (Module 4)
Advanced Java
Module 4
1|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
---------------------------------------------------------------------------------------------
Spring and AOP :
Aspect Oriented Programming with Spring, Types of advices, Defining Point Cut Designator,
Annotations.
----------------------------------------------------------------------------------------------
Aspect Oriented Programming (AOP) compliments OOPs in the sense that it also provides
modularity. But the key unit of modularity is aspect than class.
AOP breaks the program logic into distinct parts (called concerns). It is used to increase
modularity by cross-cutting concerns.
A cross-cutting concern is a concern that can affect the whole application and should be
centralized in one location in code as possible, such as transaction management,
authentication, logging, security etc.
OOP promotes reusability and flexibility of code already. So, why then do you need AOP? AOP
is a programming paradigm that has all the benefits of OOP as well. Added to this, you can
promote loose coupling and enable your application to use pluggable aspects as and when
needed sans any change in your application's code. In using AOP, you can focus on the business
logic of your application while at the same time weave the aspects to the business logic. One
of the major benefits of using AOP is that you would just need to write your aspects once and
then you can reuse it wherever you need to in your application. So, AOP is a great way to
reduce the complexity of the source code of your application and make your code clean.
It provides the pluggable way to dynamically add the additional concern before, after or
around the actual logic. Suppose there are 10 methods in a class as given below:
class A{ public void m1(){...} public void m2(){...} public void m3(){...} public void
m4(){...} public void m5(){...} public void n1(){...} public void n2(){...} public void
p1(){...} public void p2(){...} public void p3(){...}
2|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
}
There are 5 methods that starts from m, 2 methods that starts from n and 3 methods that
starts from p.
Understanding Scenario I have to maintain log and send notification after calling methods
that starts from m.
Problem without AOP We can call methods (that maintains log and sends notification) from
the methods starting with m. In such scenario, we need to write the code in all the 5
methods.
But, if client says in future, I don't have to send notification, you need to change all the
methods. It leads to the maintenance problem.
Solution with AOP We don't have to call methods from the method. Now we can define the
additional concern like maintaining log, sending notification etc. in the 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.
AOP concepts
Let us begin by defining some central AOP concepts and terminology. These terms are not
Spring-specific.
• 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.
3|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
• 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.
• Advice : This is the actual action to be taken either before or after the method
execution. This is an actual piece of code that is invoked during the program execution
by Spring AOP framework.
• 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 : The object being advised by one or more aspects. 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.
Types of advice:
4|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
• after-throwing : Run advice after the a method execution only if method exits by
throwing an exception.
• Around : Run advice before and after the advised method is invoked.
Wildcard Definition
5|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
cover the starting with the type signature expressions that could be used for package name–
or class name–based filtering; method signature expressions that could be used for filtering
methods based on their actual signatures; and bean name expressions that could be used for
filtering methods, which reside in a bean given with a name pattern. It’s also possible to
blend the expressions with grammatical operators: and, or, and not (or with &&, ||, and !).
So the sky is the limit in the designator world! The Type Signature Expressions For filtering
methods according to its types—like interfaces, class names, or package names—Spring AOP
provides the within keyword. The type signature pattern is as follows, and type name could
be replaced with package name or class name. within(<type name>) Here are some
examples for the type signature usages:
➤ within(com.test..*): This advice will match for all the methods in all classes of the
com.test package and all of its subpackages.
➤ within(com.test.spring.ch.MyService): This advice will match for all the methods in the
MyService class.
➤ within(MyServiceInterface+): This advice will match for all the methods of classes that
implement the MyServiceInterface.
➤ within(com.test.spring.ch.MyBaseService+): This advice will match for MyBaseService
class and for all of its subclasses.
6|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
7|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
AOP Implementations
Spring AOP
Spring AOP can be used by 3 ways given below. But the widely used approach is Spring
AspectJ Annotation Style. The 3 ways to use spring AOP are given below:
8|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
Aspects are implemented using the regular classes along with XML based
configuration.
1. Create a project with a name SpringExample and create a package com.test under the
src folder in the created project.
4. Create Java classes Logging, Student and MainApp under the com.test package.
6. The final step is to create the content of all the Java files and Bean Configuration file
and run the application.
Here is the content of Logging.java file. This is actually a sample of aspect module which
defines the methods to be called at various points.
9|Page
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
//This is the method which I would like to execute when any method returns.
public void afterReturningAdvice(Object retVal) {
System.out.println("Returning:" + retVal.toString() );
}
//This is the method which I would like to execute if there is an exception raised.
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("There has been an exception: " + ex.toString());
}
}
Following is the content of the Student.java file
10 | P a g e
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
<aop:config>
<aop:aspect id = "log" ref = "logging">
<aop:pointcut id = "selectAll" expression =
"execution(* com.test.*.*(..))"/>
11 | P a g e
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
</beans>
12 | P a g e
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
1. Create a project with a name SpringExample and create a package com.test under the
src folder in the created project.
4. Create Java classes Logging, Student and MainApp under the com.test package.
6. The final step is to create the content of all the Java files and Bean Configuration file
and run the application.
Here is the content of Logging.java file. This is actually a sample of aspect module which
defines methods to be called at various points.
import org.aspectj.lang.annotation.*;
//This is the method which I would like to execute before a selected method execution.
13 | P a g e
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
@Before("selectAll()")
public void beforeAdvice(){
System.out.println("Going to setup student profile.");
}
//This is the method which I would like to execute after a selected method execution.
@After("selectAll()")
public void afterAdvice(){
System.out.println("Student profile has been setup.");
}
//This is the method which I would like to execute when any method returns.
//This is the method which I would like to execute if there is an exception raised by any
//method.
14 | P a g e
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
student.printThrowException();
}
15 | P a g e
C-4, Wagle Industrial Estate,
Near Mulund Check Naka,
Thane West, opp. Aplab, Mumbai,
Maharashtra 400604
Developed by:- Prof Abhay More
}
Following is the configuration file Beans.xml
<aop:aspectj-autoproxy/>
</beans>
16 | P a g e