In previous post link I talked about aspects, concretely ITD, and how can be used to design your classes with its own responsibilities while maintaining source code clear and concise. In that post I used aspectj and spring-aspects as aspect-oriented implementations. An important concept of aspect programming is the process of weaven. An aspect weaver takes information from raw classes and aspects and creates new classes with the aspect code appropriately weaved into the classes.
If you are using Eclipse with AJDT plugin, weaver is executed automatically, but if you are using any build tool like Maven, you should take care of configuring correctly so generated classes contains aspect code too.
In this post I will explain how I have modified a pom file so compilation process also weaves aspect code into classes.
First thing I always do when I generate a pom file is adding component version in properties section. In this case spring and aspectj:
Next step is adding dependencies. To work with aspectj and spring-aspects four dependencies must be added.
But one more important dependency is required, and let me surprise you:
You can think I am joking, but not, JPA API is required. Here you can read why: https://jira.springsource.org/browse/SPR-6819. AnnotationDrivenStaticEntityMockingControl class requires javax.persistence.Entity to be in classpath. Hopefully nowadays most projects use JPA, so this dependency may already be required by your code, if not, then you should append it as dependency.
And finally aspectj-maven-plugin is registered:
This plugin requires that you define which dependencies will be used using <dependencies> tag. In order to apply already compiled aspects to your own sources you need to setup all JAR files you would like to weave in the plugin configuration using <aspectLibraries> section; in this case spring-aspects artifact is required so application can use capabilities that offer @Configurable annotation. Finally execution section should contain compile and test-compile goals so main and test classes can be woven.
Hope you find this post useful.
Alex.
You nedd just aspectjrt runtime dependency when you are weaving during compile.
ResponderEliminarUnless your project already depends on it, you should add the dependency on hibernate-jpa-2.0-api with scope "provided", as shown in the Jira issue you linked to.
ResponderEliminarHi Alex,
ResponderEliminarNice blog! Is there an email address I can contact you in private?
Yes you are right, jpa could be added with scope "provided", but because me projects more often than not requires jpa, I have as I showed, and I forget to note that. Thank you very much
ResponderEliminar