Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

xml based vs annotation based  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wondering if spring supports a hybrid mix of xml based and annotation based code ? For example, if I define some <bean> in applicationContext.xml and define some other beans using annotation in their java class file, does Spring take that ?
 
Ranch Hand
Posts: 73
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can mix.
In Spring MVC, I have used an .xml config file, while annotating components with @Controller, @Service and @Repository.
 
Bartender
Posts: 19365
86
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spring, like Hibernate and I'm sure like several other platforms, started out using XML metadata files.

This was a real nuisance, since every time you touched something, there was a potential need to edit two files to do one task.

The introduction of annotations in Java 5 meant that the need for external definition of metadata was much reduced. However, the old XML forms have been kept for 2 reasons:

1. Backwards compatibility. Something that Java is expected to excel in.

2. Default/override mechanism. This allows you to define default meta-properties in the class source, but override them for specific instances. That means that you can wire in (re-use) objects with a minimum of external configuration, and it means that you can have multiple instances of the class where XML sets distinct properties for each instance. For example, the instance name.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, new version suggests that, No XML based configuration. only java based annotations.
 
Tim Holloway
Bartender
Posts: 19365
86
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vinoth sathi wrote:Now, new version suggests that, No XML based configuration. only java based annotations.



As I said in item 2, above, XML overrides annotations. Annotations are the preferred mechanism, but XML allows the flexibility to override annotations without making source code changes to the class. Or worse, having multiple instances where you'd otherwise have have to explicitly re-code the class for each case of different annotation values.
 
Rancher
Posts: 948
9
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, horrid that XML.  Still useful when using spring integration as you can then use their STS diagram tool.

Never really sure when people say annotation configuration, as its my understanding that there are two types; Java configuration and component scanning - both use annotations.  Java config was the preferred way, so you still have the configuration in one central place, etc...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!