【问题标题】:why do I need the prefix 'batch:' in my spring beans为什么我的spring bean中需要前缀'batch:'
【发布时间】:2013-07-07 07:00:44
【问题描述】:
我在 Spring Batch 示例中随处可见标签所在的位置(即 <job></job>)。但是在我的 xml 文件中,我必须包含“批处理”。例如 <batch:job></batch:job>
这是为什么呢?那是版本的事情吗?如果可能的话,我想通过删除 batch: 来精简标签。
【问题讨论】:
标签:
xml
spring
xml-namespaces
spring-batch
【解决方案1】:
您可以将 xmlns="http://www.springframework.org/schema/batch" 添加到根元素并避免前缀,例如:
<flow id="flowId" xmlns="http://www.springframework.org/schema/batch"><step id=""/><step id=""/></flow>
【解决方案2】:
batch 是您的 XML 文件中 http://www.springframework.org/schema/batch 命名空间的别名。在你的 XML 开始时你会得到这样的东西:
xmlns:batch="http://www.springframework.org/schema/batch"
这意味着,每当您为元素添加 batch: 前缀时,您就指定该元素是 Spring 定义的元素。这对于消除可能的歧义是必要的(可能还有其他框架围绕定义 <job> 元素)。
可以为 XML 文档中的所有元素定义一个默认命名空间,这样如果没有命名空间前缀,它将是声明所引用的命名空间。这个默认命名空间是使用xmlns="..." 属性定义的,通常分配给http://www.springframework.org/schema/beans 命名空间(<beans> 元素和更多基本类型所在的位置)。
你可以:
1) 如果要清理,请将 batch 更改为更短的名称,例如 b
<beans ... xmlns:b="http://www.springframework.org/schema/batch">
...
<b:job></b:job>
...
</beans>
2) 将batch 设为默认命名空间(使用xmlns="http://www.springframework.org/schema/batch")并在需要时使用beans 命名空间和其他命名空间。您甚至可以在一个单独的 xml 文件中声明所有批处理元素,并使用此命名空间作为默认命名空间,并将 <beans:import> 声明在您的主 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>