0

Sorry to ask this question which may be already asked in Stack Overflow for different situation.

I searched a lot in Stack overflow and google . But couldn't find a reliable and a well suitable answer. That's why I made a new question.

After some search , I found that its better to use both Annotation and XML configuration in the Spring project. So now I have some questions to ask ,

  1. Am I found the correct one?

  2. Also can anyone tell me when to use annotations and when to use XML based configuration in Spring?

  3. Some pros and cons for both `annotaion and XML configuration with example situations ?

Good answers are definitely appreciated.Thanks.

Human Being
  • 8,269
  • 28
  • 93
  • 136
  • 1
    Take a look at following question http://stackoverflow.com/questions/182393/xml-configuration-versus-annotation-based-configuration And http://stackoverflow.com/questions/8428439/spring-annotation-based-di-vs-xml-configuration – Jabir Mar 29 '13 at 06:57

1 Answers1

3

I follow the following :

I use annotations for classes in my own project and resort to XML when I need to inject beans in 3rd party libraries cause XML is the only way I can do that.

I'd suggest using annotations cause they are much much easier to maintain as compared to XML definitions.

Plus spring facilitates a super simple way of auto registering beans using component scanning that uses annotations.

Plus it's easier to do testing cause you don't need to keep editing XMLs for injecting dependencies for your unit tests.

Plus annotations act as documentation for the behavior of your code.

Keep in mind that XML definitions will override annotations.

Varun Achar
  • 14,781
  • 7
  • 57
  • 74