Benefits of JavaConfig over XML configurations in

2019-02-04 00:48发布

问题:

Earlier the configurations used to be in hard coded in the code, later it was externalized to .property files (for sake of avoiding hard coded values, avoiding changing code for the sake of changing configurations..etc) then it moved to XML (for sake of being more standardized, error free..etc)

Now, while reading about @Configuration in Spring 3 , looks like we are again moving back to the initial approach.

Why would we want to hard-code configurations in the code rather than having it externalized ?

回答1:

There are some advantages

  1. Java is type safe. Compiler will report issues if you are configuring right bean class qualifiers.
  2. XML based on configuration can quickly grow big. [Yes we can split and import but still]
  3. Search is much simpler, refactoring will be bliss. Finding a bean definition will be far easier.

There are still people who like XML configuration and continue to do it.

References: Java configuration advantages Some more reasons