Starting from Java5, Java starts a trend of annotation-based development. Java gives you ability to build your custom annotations as required and build your configuration through annotations.
In one of my old projects I created custom transaction manager using custom Java annotations and Aspects. I used AspectJ library together with Spring Framework for creating Aspects that run over custom annotations defined as point-cuts.
Next, we will iterate on how we can create aspects run over custom annotations using AspectJ and Spring Framework. This will be a simple example of creating custom transaction manager for starting, committing and rollback.
- To create custom annotation, you will create annotation interface and define its target (METHOD, FIELD, PARAMETER, TYPE, etc). The next one is MyTransaction annotation that will be used to annotate transactional methods.
- Creating aspect using AspectJ and Spring Framework is based also on annotations. You have annotation called “@Aspect” used to annotate classes that will be waved as aspects using AspectJ Weaver. You will have aspect for your Transaction Manager as following :
- This aspect will have point-cuts over your custom annotation. This will be done as following:
- After creating your point-cut, you need to create your aspect advice(s). Each advice is just a method that will run at specific point-cut at some time (before, after or around).
- The following advice will run at point cut “myTransactionMethod” and before running the method have this point-cut (the methods annotated with “MyTransaction”). As you see from its name it will start new transaction.
- This advice will run after returning from the point-cut. As you see from its name its code will commit the last opened transaction.
- Well, your transactional method may fail and need to rollback. Then, you will need to have advice run after throwing exception at your point-cut.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyTransaction {
}
To use it, just add annotation “@MyTransaction” before any method you want to be handled using your transaction manager:
@MyTransaction
Public void doCode(){
}
@Aspect
public class MyTransactionManager {
}
@Pointcut("execution(@MyTransaction * *(..))")
private void myTransactionMethod() {
//No code needed here, just empty method
}
The last method defines point-cut over all methods have annotation “MyTransaction”. You can imagine this point-cut as place holder at specific parts of your code.
@Before("myTransactionMethod()")
public void doStartTransaction() {
//code for starting transaction
}
@AfterReturning(pointcut = "myTransactionMethod()")
public void doCommitTransaction() {
//code for committing transaction
}
@AfterThrowing(pointcut = "myTransactionMethod()")
public void doRollbackTransaction() {
//rollback transactional code
}
The last example is just starting point for you to know how you can use Java Custom Annotations together with Aspect Oriented Programming to create a great concern separated components like the one we do.
I attached the two example classes, MyTransaction.java annotation and MyTransactionManager.java aspect to this post.
No comments:
Post a Comment