Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do I need the prefix 'batch:' in my spring beans

I see everywhere in Spring Batch examples where the tags are (i.e. <job></job>). However in my xml files I have to include 'batch'. For example <batch:job></batch:job>

Why is that? Is that a version thing? I'd like to thin out the tags by dropping batch: in all of them if that's possible.

like image 317
Davidson Avatar asked Jul 09 '13 15:07

Davidson


1 Answers

batch is the alias in your XML file for the http://www.springframework.org/schema/batch namespace. You'll have something like this at the beggining of your XML:

xmlns:batch="http://www.springframework.org/schema/batch" 

This means that whenever you're prefixing an element with batch:, you're specifying that the element is the one Spring defines. This is necessary to clear out possible ambiguities (there might be other frameworks around defining the <job> element).

It is possible to define a default namespace for all elements in the XML document, so that if no namespace is prefixed, it'll be the one that the declaration is referring to. This default namespace is defined with the xmlns="..." attribute, and is usually assigned to the http://www.springframework.org/schema/beans namespace (where the <beans> element and many more of the basic types reside).

You could either:

1) Change batch to something shorter such as b if you want to clean up

<beans ... xmlns:b="http://www.springframework.org/schema/batch">
    ...
    <b:job></b:job>
    ...
</beans>

2) Make batch the default namespace (with xmlns="http://www.springframework.org/schema/batch") and use the beans namespace and others when needed. You could even declare all your batch elements in a separate xml file with this namespace as default, and <beans:import> it in your main applicationContext.xml.

<beans:beans xmlns="http://www.springframework.org/schema/batch" 
     xmlns:beans="http://www.springframework.org/schema/beans">

    <job>...</job>
    <job>...</job>

</beans:beans>
like image 107
Xavi López Avatar answered Sep 23 '22 15:09

Xavi López