• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

xml based vs annotation based

 
Greenhorn
Posts: 28
  • 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: 86
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: 20982
128
Android Eclipse IDE Tomcat Server Redhat Java 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: 25
  • 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: 20982
128
Android Eclipse IDE Tomcat Server Redhat Java 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.
 
Bartender
Posts: 1035
18
Mac OS X IntelliJ IDE Oracle Spring VI Editor Tomcat Server Redhat Java Linux
  • 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...
 
Those are the largest trousers in the world! Especially when next to this ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!