As we know the Aspect
is just the name of the cross-cutting
functionality, not the implementation. So what’s the implementation of the cross-cutting
functionality? It’s called Advice
. An Advice
provides the code for implementation of the service. It is like Logging service, Logging is a Aspect
and Advice
denotes the implementation of Log4j.
There are four types of Adivce
:
- Before Advice
- After Advice
- Around Advice
- Throws Advice
In this article, we only explain Before Advice
. The other advices we will talk about it in later blogs.
The Before Advice
is the implementation of the service. It will be applied before the business logic of method is going to execute. So this means this advice will execute before the business logic. we can use it to do something like authentication before the business logic execution.
It will be applied in the runtime of application, not the compilation of the code. And also it will generate a new proxy class which is combined by this advice and business logic class in the runtime automatically.
When we create a Before Advice
, we should implement the MethodBeforeAdvice
interface, this interface is given by org.springframework.aop.* package. And then we need to override the before
method of it.
1 | package spring.test.aop; |
There are 3 parameters in the before method.
The first parameter Method
method is used to access the method name of the business logic via getName()
.
The second parameter Object[] args is Object array, it is used to access the arguments of the logic method.
The last parameter Object target is an object to whom this service will be going to apply, usually this will taken care by container, actually we no need to care it.
Okay, let’s see a full example.
1 | <dependencies> |
In the pom.xml file, we add above dependencies to our project. (this project use maven to manage)
Okay, let us create our logic class, firstly we need to create interface for it.
1 | package spring.test.aop; |
We created two method in this interface, this interface is very important for our AOP
project.
1 | package spring.test.aop; |
Our class implements the interface, just print the result of the add or subtract.
For now, we have our logic class. but now we want to do something before the method logic is going to execute. So the Before Advice
is comming.
1 | package spring.test.aop; |
We print some messages in our Before Advice
, this will be printed before the logic method result.
Finally, we need to make the logic class and our Before Advice
are combined together. so we need to config them in the xml file.
1 |
|
In AOP
always we need to create a spring bean in the form of interface and implementation class only, because the IOC container internally creates proxy class by implementing that interface with the help of ProxyFactoryBean
.
To run this application, we need a class which with main method. See below codes.
1 | package spring.test.aop; |
In the client application, we are passing id of ProxyFactoryBean
(proxyFactoryBean) to get the object, because we need proxyed object to invoke our logic method.
The result of the execution is below:
This will be executed before the add method
10 + 20 = 30
This will be executed before the subtract method
10 - 5 = 5
So, it is simple to use it. and we separated the logic and the services completely, only need to config the xml file when the services changed.