1

I am new to Spring framework and have seen annotations at many places. I understand Built-in Java Annotations like @Deprecated, @Override, @SuppressWarnings.

I have below questions:

  1. Does one need to understand creating custom annotations to understand Spring framework?
  2. For what purpose annotations are required?
Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
hsingh
  • 661
  • 5
  • 26
  • Annotations are an alternative to XML configuration. Spring supports both. You can use one or the other or a mix. – duffymo Apr 30 '15 at 09:37

4 Answers4

1

Annotations are used to describe elements and clarify their meaning. Prior to their inclusion, that information had to be kept somewhere else, generally a file.

Also, knowing how a java feature works is always useful. So despite you don't need to know how to create your own annotations, it might give you some insight on the internals.

Have a read here:

How and where are Annotations used in Java?

Community
  • 1
  • 1
Tavo
  • 3,087
  • 5
  • 29
  • 44
  • That's a pre-canned auto-comment but "here" means in your answer. It means post the link and summarize the contents so that you can get the gist even without clicking on it. – samgak Apr 30 '15 at 13:04
  • Ok, I had never seen that auto-comment before. Noted for future answers and edited the answer. – Tavo Apr 30 '15 at 13:09
0

For the first question: You do not need to know how to write an annotation to use it.

For the second:

Annotations are used for many different reasons:

  • To execute code during runtime based on annotations (e.g. @Transactional in spring)
  • To create different code during compile dependeing on the annotation (e.g. @AspectJ)
  • To evaluate code (e.g. javac or FindBugs)
  • ...

There are many things that could be done with annotations.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
0
  1. You need not know about the custom annotations in order to learn Spring annotations.
  2. For Spring annotations, you may start with this link and then explore as you learn further. It explains the usage/need to each of the Spring annotations to get started with.
Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
0

While not actually answering your questions (which others already answered), this should give you enough information to comfortably use them.

For annotations to actually be useful, you need code which looks for them and handles them accordingly. The same goes for writing your own custom annotations.

Simplified example with @Transactional should make things clear for you. When you put @Transactional on a bean method, there is some Spring code which scans these beans and methods, and picks up your annotated method. Whenever that method is called (won't go into proxies right now), Spring opens a transaction, executes your method, and closes the transaction. You get all of that just by putting annotation on your method.

So, each annotation comes with code that handles it's wanted behavior.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68