Aspect Oriented Programming is just another programming orientation which helps you increase modularity level in your code by grouping the same requirement / behavior / concern into the same module and inject them to the code without modifying any existing code. I think this concept is equivalent with High Order Component, Middleware, or Decorator in other programming language.
Guice made by google to facilitate dependency injection, service binder, method interceptor, etc. We can use method interceptor feature to easily implementing AOP. But, Guice has several limitations, such as:
- Classes must be public or package-private.
- Classes must be non-final
- Methods must be public, package-private or protected
- Methods must be non-final
- Instances must be created by Guice by an
@Inject-annotated or no-argument constructor. It is not possible to use method interception on instances that aren't constructed by Guice.
There are at lease 4 terms you need to know before implementing Aspect Oriented Programming, such as:
Cross-cutting concerns is referring to what kind of concerns / behaviors / requirements that can be grouped. Let’s say we have some method that need to be logged every time it’s called then we can call it logging concern, or maybe you need to authenticate user credentials on API call, you can call it concern too.
Advice in aspect oriented programming is the code of the concern that you want to embed to the code.
Pointcut is referring to the point of execution that the advice will be injected. Let’s say we want to inject the logging advice to every method that has annotation
@Loggable, or you want to inject the logging advice to any method without arguments. That’s pointcut.
Aspect is simple
Aspect = Pointcut + Advice .
Let’s use the easiest example. We want to create Logging Aspect and inject it to every method with
Creating annotation called
Loggable with target
Function because we want to use this annotation on method only, by default this annotation Retention is
RUNTIME so we don’t need to modify it.
Creating the advice by implementing
MethodInterceptor interface which provided by Guice, you are force to implement
invoke function with
MethodInvocation as a parameter.
Invocation is your method, by using
invocation.proceed() you are invoking your method that using
@Loggable annotation. So, any line code before
invocation.proceed() is invoked before your method, and any line code after
invocation.proceed() is invoked after your method is called.
invocation.proceed() will return the result of your method.
Creating Aspect and Defining Pointcut
Aspect is advice + pointcut, so by defining the point cut and defining what advice will injected to that pointcut, you are creating the aspect. By Guice, you just need to create a class that extends
AbstractModule so you need to override
configure method. Using
bindInterceptor, you can define the class pointcut, method pointcut, and defining the advice.
Creating Main Application
- Declare Main Interface and its implementation
Remember the limitation
Classes must be non-final, by default Kotlin class is Final, adding open keyword to the class means, it is no longer final class. Don’t forget to add the Loggable annotation.
2. Register Main Interface and its implementation to guice due to this limitation
Instances must be created by Guice.
3. Run the application and create the instance with guice
4. Logging advice will run
Thank you for reading!
In computing, aspect-oriented programming ( AOP) is a programming paradigm that aims to increase modularity by allowing…
Documentation: User Guide, 4.2.2 javadocs, Latest javadocs Continuous Integration: Mailing Lists: User Mailing List…
Carbon is the easiest way to create and share beautiful images of your source code.
To join our community Slack team chat 🗣️ read our weekly Faun topics 🗞️, and connect with the community 📣 click here⬇