0

In Java, is it a good practice to use annotations to configure an application rather than using XML files? I am more skeptical about it because, using annotations involves changing the java source files and it is as good as declaring constants in java files and then using those constants, whereas when we make the configurations using XML files, we can keep all the configuration changes away from java source files and keep the configurations in separate XML files, this approach sounds more neat. Also, when we need to make changes to configuration, we know which XML file to change rather than searching the java files for the annotations. Also, we can update an XML file in an EAR without compiling the code again, whereas if we make any change in an annotation, then we need to compile the code again.

Can anybody please throw some light on why should we use annotations and not XML files for configuration?

Gaurav
  • 1,570
  • 4
  • 20
  • 25

5 Answers5

1

Which to use may vary depending on what's being configured, how the configuration is used, project/cultural conventions, etc. Good IDE support makes using either more convenient and reliable.

Personally, while I use both XML and annotations, I tend to prefer XML configuration for many tasks, particularly on larger projects. For example, with Spring, I prefer XML configuration: it's easier to manage the configuration itself, configuration changesets, and environment-specific changes (e.g., testing, server-based, etc.), when it's more localized.

For other configurations, annotations are often more appropriate and convenient. For quick projects with little or no domain class customization, Hibernate annotations may be more convenient.

Ultimately it's a matter of preference and convenience rather than a purely technical one. (Except when the XML and annotations support different features; sometimes one offers more-complete capabilities.)

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

I prefer annotations since my IDE can help me validate my configurations. Configurations stored in xml-files cannot be validated before runtime (I'm thinking mostly about spring and injections)

Also, I find that for anything more than a tiny project, a large xml-config is hard to maintain.

slipset
  • 2,960
  • 2
  • 21
  • 17
0

When working with annotations, you have to take care of only one place to configure your stuff (java code). When configuring with XML, many times a programmer can "forget" to configure a new property or class in the XML, and after the error must correct and restart, resulting in a waste of time.

greuze
  • 4,250
  • 5
  • 43
  • 62
0

I would say it very much depends on what you are configuring.

If configuration maybe or should be changed after deployment, then in most cases it is preferable to use XML (or other text-based format). This would include Hibernate server configuration, Tomcat/Jetty configuration, Log4j configuration, etc. The main advantage is flexibility.

For cases when configuration does not need change after deployment, configuration using annotations is preferable. Too many configuration files also complicate your software, so it's best to keep it to a minimum. Good examples would be of annotation-based configurations: Hibernate bean mapping configuration, Spring dependency injection, Guice, etc (some give you both options, but I would prefer annotations here). The advantage is better manageability, and compile-time checking for errors (this depends on the API, of course).

rodion
  • 14,729
  • 3
  • 53
  • 55
0

Personally, when I've tried to understand a new system, having the annotations right there with the code makes it easier to follow and comprehend. Hunting for references of the class in configuration files can be a little annoying.

aldrin
  • 4,482
  • 1
  • 33
  • 50