What are the actual perks of using an annotation that adds a particular functionality to a class or a block of code (other than rapid development purposes of course)? This may come down to preferences but I do not use annotations at all, as they can put a limit on the developer. One can easily lose the freedom of customization.

My specific situation.

We're a big eCommerce Platform based on Spring Framework. Now obviously we have better choices such as Spring4 but we still use the deprecated (Spring 2.5) JAR just because I was able to extend the SimpleFormController class and add custom functionality to it. Now that JAR is not even available in the Maven's repo. Here, we took a big risk and chose the deprecated API when better alternatives are available.

As I previously mentioned, this question is more related to preferences. Can I know where annotations are best used and what are its actual perks, just considering my point of view?

  • This question isn't specific enough to be answerable. Decisions like this are made by evaluating the merits of each alternative in the context of your specific requirements, and making a choice. That said, annotations are part of the Dependency Injection portion of Spring; you use them for the benefits that annotated dependency injection provide. – Robert Harvey Aug 28 '16 at 16:38

This is a very generic question - inheritance and annotations are two completely different things. In general, you use inheritance when creating a new class for two reasons: to inherit functionality, and to make it a subtype. Together, and complemented by features such as method overriding, these two items make for a powerful way of code reuse and extensiblity.

Annotations, on the other hand, are just a way of adding custom declarations to classes (and other code entities, of course). Frameworks, such as Spring, may interpret them as they want.

This is the point, where these two overlap: frameworks may choose to use annotations to allow code reuse and extensibility as well. Spring, for example, uses annotations to add a particular functionality to a class or a block of code, as you say, for example via AOP.

So, your question may actually be whether to use AOP vs. subclassing/inheritance. And the answer to this is: it depends.

There is one main problems of inheritance: you can only choose a single base class. So, if you want to reuse and extend functionality from multiple different components, subclassing simply doesn't work. As a second problem, you also cannot reuse functionality via inheritance without the respective class becoming a subtype. Since subtyping implies substitutability, inheritance might actually add more constraints than you want or need.

Instead, in pure OOP, you're often told to use composition over inheritance. By composing components rather than inheriting from them, you circumvent the single base class problem, but this comes at the cost of

  • more boilerplate code and
  • less extensibility - you can't override protected methods any more.

Frameworks like Spring help you reduce boilerplate code by automatically composing components for you, and they provide aspect-oriented programming features to give you more extension points. So, they'll give you the benefits of inheritance (and more), but without the single base class problem. However, all is not great - this instead add some complexity to your code; one needs to understand the framework and its concepts to understand how Spring will interpret annotations. Reasoning about how your code will behave might become more difficult.

So, which one to prefer? Here are some guidelines I can come up with.

  • Don't inherit from classes not meant for subclassing. Use their intended extensibility mechanism, if there is one.
  • Favor object composition over inheritance. Use inheritance mainly for subclassing (i.e., to achieve substitutability), not for reuse.
  • Don't use AOP if there is a simpler alternative.
  • Use AOP and other annotation-based features if it makes your code more declarative and easier to read.
  • Read the documentation of your frameworks and try to follow their best practices.
  • Nice information. I was stupid to ask such a generic thing, but that had made a design pattern clear in my mind. And also what I get, I could have gone with the annotation based spring, and I could have my own annotation that mimics the custom functionality which I have added. Am I getting it right, sir? – Jay Aug 29 '16 at 17:25
  • You might be able to use Spring AOP to achieve the same goals as your subclassing solution that no longer works, yes - but that depends on your specific issue. Maybe ask on StackOverflow how to solve your specific coding issue? And I don't think you were stupid at all :) – Fabian Schmied Aug 30 '16 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.