From the course: Spring: Framework in Depth

Aspecting in Spring

From the course: Spring: Framework in Depth

Start my 1-month free trial

Aspecting in Spring

- [Instructor] One of the coolest features with Spring in my opinion is how easy it is to leverage aspects to add global behavior to your application. Let's talk a little bit about what aspects are. Essentially, they're reusable blocks of code that you can inject into your application at runtime. These are very powerful tools for adding behavior, especially behavior that applies to cross-cutting concerns, because you can write the code in one spot and automatically apply it to many locations throughout your application. So what are some of the common applications of aspects in modern application development? First and foremost is logging. Everybody needs logging, needs metrics, needs tracing, and I'm kind of wrapping these up into one block. And this allows you to apply a default set of rules to every method or a subset of methods based on whatever your criteria may be. Transaction management is another common use of aspects across the applications base, and in fact, Spring provides an aspect for you at transactional to do this work. And it's an aspect that's applied on top of an adaptation, which in my opinion, is the easiest way to implement an aspect in the Spring world. Caching is another very useful opportunity for aspects. And again, Spring provides an annotation to do this. But I find that sometimes that I like to have my own custom caching app behavior, so I often have written my own in certain use cases where I've needed to abstract caching across a broad swath of applications. And how can we stop without talking about security? Security is one of those things that gets applied all over the place, but it's a pain to write. So by using aspects for security things like encryption and decryption and transformation and hashing and logging security events, which kind of crosses a little bit of the boundary there. But by doing that with an aspect, we can apply everywhere and right at once. Which alludes us to why. So, imagine a logging routine that applies to every service method. Now, if you do this in code every single time, you've got code duplication. And we have this method of DRY, or don't repeat yourself, that we'd be violating if we did this logging routine in every service method. You're also kind of mixing concerns in this case. You have a service method that's doing some business functionality, and we have to break out of the business functionality to apply a logging routine, when in reality, we should just be maintaining our application logic. And by using aspects, we can apply the logging routine without it ever being in our code. So let's talk about how Spring uses aspecting. So, first and foremost it's AspectJ-based. They use AspectJ for all of their aspecting. They do byte code modification, which is run time interweaving. AspectJ actually has a lot more ways of doing aspects, including compile time interweaving, but Spring does it through run time interweaving. It's all dynamic proxy-based. So remember, Spring is all proxy-based. There's a reason for that. It's cause a lot of its behavior is done through aspects. So, Spring aspects should behave the same as every other proxy within the system. They're dynamic proxy-based aspects. There are several things that we need to talk about from a terminology perspective when dealing with an aspect, especially a Spring aspect. First and foremost is a join point, and this is the spot in the execution of code where a cross-cutting concern can be applied. This is in your code, and it's not your aspect code, this is in your business logic. A pointcut is a routine used as the selection criteria to select the join point to apply that crosscutting concern. Those two can get mixed up a lot. So a join point is your code. The pointcut is what selects your code. The advice is the code that is then applied to the join point when selected by the pointcut. This is the code that applies the crosscutting concern logic. So, that logging routine would be executed in advice that is selected by a pointcut to a join point that is your application code. And an aspect in Spring is the entire package. So it's all of this, it's the advice and the pointcut and ultimately the join point that that advice is applied to.

Contents