1

I am starting to learn about the Spring framework. I have seen two ways to configure a web application, one uses a web.xml to configure the servlets ect. The other uses a Java class that implements a WebApplicationInitializer and is annotation driven.

I was told that, "XML is the old way, no new project should use XML anymore". Can anyone tell me why this is? Lots of online resources quote "Convention over Configuration", however, using the online learning tools I have access to, the vast majority of examples are using xml configuration. I am finding it very difficult to find relevant examples with Java configuration.

I would also like to know what the pro's and con's of using one over the other are? If it it easier to find resources based around xml configuration, then would it be destructive to future job prospects to side step the Java configuration and focus on xml?

Chris
  • 103
  • 3
  • 15
  • 2
    `XML is the old way, no new project should use XML anymore` Disagree 100 percent. It depends on the project. Spring beans are far easier to inject and create using Java config. Configuration of large projects is much easier to manage in XML. A small webapp or service can use pure Java config, and it might be no problem. Managing pure Java config on a large webapp would be a nightmare. (All my opinion, of course) – Christopher Schneider Mar 07 '17 at 16:54

1 Answers1

0

The advantages of Java Config is the type safty. The Compiler can check if you wire your application correctly (based on the types). Refactoring is a little bit easier.

Using XML configuration this can only checked during runtime.

My own opinion is that there is not a big difference between the two approches. You only "tell Spring" differnetly how to wire the application. The Java Config brings in some nice features (e.g. Spring Security Config), but also hides some "magic" which is sometimes harder to understand.

You may also have a look at earlier questions about this topic.

In Projects we still do here a lot of XML configuration which works pretty well. New Configs are often written as Java Config and integrated into the "lagacy" configs.

Community
  • 1
  • 1
thopaw
  • 3,796
  • 2
  • 17
  • 24
  • Interesting to know that Java config gives type safety, have you got any suggestions on where I can look to find implementations of java config? for example, an OpenEntityManagerInViewFilter to avoid LazyInitializationException's uses a `` tag. And I can not find the equivalent implementation for Java using annotations. – Chris Mar 07 '17 at 16:57