What's good about writing Java code in XML like Spring configuration?

As a .NET developer who has launched Java-dev for less than a year, one thing that puzzles me is the extensive use of the Spring config file. Let me clarify:

In the case of the IoC container, I have not seen the community interested in creating their own directory / module / Etc. via xml config on any platform other than Java.

XML configuration is generally used as a verbose alternative to constructor / factory calls. This is clearly a flaw in the code as it is not text-safe, too verbose and not indexed in the IDE (for example, "Finding a Method Usage")

Other IoC frameworks like xof configuration support for Autofac, but in those non-java platforms, XML configuration is unpopular.

My question is:

Is there a best / practical design principle etc. that supports this choice of XML configurations for IoC, or is it just a historical habit?

+3


source to share


4 answers


The XML and Properties files do not need to be recompiled, allowing deployment changes to be made in a server environment.

For example, you can have 2 bean implementations and a swap that you inject:

<bean id="impl1" ... />
<bean id="impl2" ... />

<bean id="dependent" ... >
    <constructor-arg ref="impl1"/>
</bean>

      

You can add or remove items to items Collection

:



<util:set id="some_set">
    <value>value #1</value>
</util:set>

      

Also, for managing unit test environments, if you have a bunch of XML files, one of which is for DB connections, and then a single XML file that you replace for your unit tests, which need internal memory, and can t connect to a real mac at dev time:

src/main/resources/
    - META-INF/spring/
        service-context.xml
        dao-context.xml
        datasource-context.xml

src/test/resources/
    - META-INF/spring/
        datasource-context.xml   // this is the test version of that context

      

+2


source


When we talk about Spring, you are not limited to XML configuration. As of version 3 of the Spring framework, you can customize everything using Java Annotations. However, you are limited. If you want to change something, you need to recompile your application.



You can find helpful hints on how to create a concise configuration using XML. http://gordondickens.com/wordpress/2012/07/30/enterprise-spring-framework-best-practices-part-3-xml-config/

+2


source


I found it somewhere,

1. The concentration is centralized, not scattered across all the different components, so you can have a good overview of the beans and their wiring in one place.

2.If you need to split files, no problem, Spring will let you do it. It then collects them at runtime through internal tags or aggregation of external context files.

3. Only XML configuration allows for explicit wiring - as opposed to autodevice. Sometimes, the latter is too magic for my own taste. Its apparent simplicity hides the real complexity: not only do we need to switch between autopilot type and by name, but it is more important to have a strategy for selecting the appropriate bean among all suitable shoots, but more experienced Spring developers. Profiles seem to make it easier, but are relatively new and known to few.

4. Several, but not least, XML is completely orthogonal to a Java file: there is no relationship between the two so that a class can be used in more than one context with different configurations.

See link for details

+2


source


XML allows you to change configuration without recompiling your program, sacrificing type safety for flexibility.

Some Java organizations also see the added benefit of allowing non-programmers such as field technicians or customers to change configuration settings.

Another potential benefit is the creation of custom customization tools (Pun?) That can allow the creation / modification of XML graphic files based on custom variations.

I personally don't like the XML-based programming world. It is too prone to runtime errors and difficult to debug with standard debuggers.

+1


source







All Articles