Aspects
- Reusable blocks of code that are injected into your application at runtime
- Powerful tools for adding behavior
- Solve cross-cutting concerns in one place
Common Applications of Aspects
- Logging
- Transaction management
- Caching
- Security
What is a Cross-Cutting Concern?
- Evaluate business requirements and look for words like every or always.
- Look for system requiremnts that apply to multiple business requirement.
When thinking about the term cross-cutting concern we need to look at what it really means.
The first place that we want to look at is a set of business requirements that have words like every or always. These tend to indicate that we have a simple set of requirements that applies to many different use cases. Another place, and one that I find more often than not can be solved with Aspecting, is system level requirements. If we have a system level requirement that says any time a user logs in, log all of the actions that they perform, or things like every time we execute a database method, we want specific logging written out to our system logs.
These are cross-cutting concerns that apply to system requirements and these are great places to solve that concern with an Aspect. Now, I just talked a little bit about that logging routine, but let's talk a little bit more about why that logging routine becomes so important. If we were to write a logging routine that executes every time data is accessed from the database, we would have to copy and paste that essential block of code every single time we need it.
That violates the principle of don't repeat yourself. So, using Aspects removes that code duplication. Another problem with using this sort of duplicated code is that now we're mixing concerns of our application method. If we have a method whose sole purpose is to go to the database and load the customer from the database, and then we add into it the security concerns and the logging concerns, we're now mixing into our database access method other concerns that don't apply to database access itself.
Why we need Aspects?
- Imagine a logging routine that applies to every service method.
- To avoid Code duplication - don't repeat yourself(DRY)
- Mixing of concerns is not best practise
- This helps to maintain application logic
Spring Aspects
- Leverages AspectJ for aspecting
- Byte code modification (run time interweaving)
- Dynamic proxy based
Parts of a Spring Aspect
-Join Point : A join point is a point in code in the program where execution of an Aspect is targeted towards. So this is your method, or your line of code or your annotation that the Aspect is going to target.
- Point Cut : The pointcut is the expression that identifies that join point through some sort of regular expression matching.
- Advice : The advice is the code that you actually execute at a join point that was selected by a pointcut. So the advice is your cross-cutting concern routine that we are applying to a join point in our application.
- Aspect : An Aspect is a module that contains all of your pointcuts, as well as all of your advice that is then injected at the run time of your application.
Define AOP pointcuts
Pointcut Syntax : designator("r p.c.m(arg))
r - return type
p - package
c - class
m - method
arg - 0 or more arguments
Common Designators
execution : Expression for matching method execution
The execution pointcut is one of the most common. You could write a pointcut that evaluates method names or patterns to execute your advice. For instance, in the data package all methods that start with get and any number of args execute the advice
within : expression for matching within certain types
You could use the within designator to specify that you want your advice applied to all types within a certain package, for instance. Say, com.google.common.service.
target : expressions for matching a specific type
The type designator allows you to say: apply some advice to a specific type, say, the customer service class
@annotation : expressions for matching a specific annotation
- Reusable blocks of code that are injected into your application at runtime
- Powerful tools for adding behavior
- Solve cross-cutting concerns in one place
Common Applications of Aspects
- Logging
- Transaction management
- Caching
- Security
What is a Cross-Cutting Concern?
- Evaluate business requirements and look for words like every or always.
- Look for system requiremnts that apply to multiple business requirement.
When thinking about the term cross-cutting concern we need to look at what it really means.
The first place that we want to look at is a set of business requirements that have words like every or always. These tend to indicate that we have a simple set of requirements that applies to many different use cases. Another place, and one that I find more often than not can be solved with Aspecting, is system level requirements. If we have a system level requirement that says any time a user logs in, log all of the actions that they perform, or things like every time we execute a database method, we want specific logging written out to our system logs.
These are cross-cutting concerns that apply to system requirements and these are great places to solve that concern with an Aspect. Now, I just talked a little bit about that logging routine, but let's talk a little bit more about why that logging routine becomes so important. If we were to write a logging routine that executes every time data is accessed from the database, we would have to copy and paste that essential block of code every single time we need it.
That violates the principle of don't repeat yourself. So, using Aspects removes that code duplication. Another problem with using this sort of duplicated code is that now we're mixing concerns of our application method. If we have a method whose sole purpose is to go to the database and load the customer from the database, and then we add into it the security concerns and the logging concerns, we're now mixing into our database access method other concerns that don't apply to database access itself.
Why we need Aspects?
- Imagine a logging routine that applies to every service method.
- To avoid Code duplication - don't repeat yourself(DRY)
- Mixing of concerns is not best practise
- This helps to maintain application logic
Spring Aspects
- Leverages AspectJ for aspecting
- Byte code modification (run time interweaving)
- Dynamic proxy based
Parts of a Spring Aspect
-Join Point : A join point is a point in code in the program where execution of an Aspect is targeted towards. So this is your method, or your line of code or your annotation that the Aspect is going to target.
- Point Cut : The pointcut is the expression that identifies that join point through some sort of regular expression matching.
- Advice : The advice is the code that you actually execute at a join point that was selected by a pointcut. So the advice is your cross-cutting concern routine that we are applying to a join point in our application.
- Aspect : An Aspect is a module that contains all of your pointcuts, as well as all of your advice that is then injected at the run time of your application.
Define AOP pointcuts
Pointcut Syntax : designator("r p.c.m(arg))
r - return type
p - package
c - class
m - method
arg - 0 or more arguments
Common Designators
execution : Expression for matching method execution
The execution pointcut is one of the most common. You could write a pointcut that evaluates method names or patterns to execute your advice. For instance, in the data package all methods that start with get and any number of args execute the advice
within : expression for matching within certain types
You could use the within designator to specify that you want your advice applied to all types within a certain package, for instance. Say, com.google.common.service.
target : expressions for matching a specific type
The type designator allows you to say: apply some advice to a specific type, say, the customer service class
@annotation : expressions for matching a specific annotation
No comments:
Post a Comment