为什么我的spring bean中需要前缀'batch:'

Dav*_*son 4 xml spring xml-namespaces spring-batch

我在Spring Batch示例中看到了标签所在的位置(即<job></job>).但是在我的xml文件中,我必须包含'batch'.例如<batch:job></batch:job>

这是为什么?这是一个版本的东西吗?我想通过删除批处理来稀释标签:如果可能的话,在所有标签中删除标签.

Xav*_*ica 8

batch是http://www.springframework.org/schema/batch命名空间的XML文件中的别名.在XML开始时你会遇到这样的事情:

xmlns:batch="http://www.springframework.org/schema/batch" 
Run Code Online (Sandbox Code Playgroud)

这意味着无论何时为元素添加前缀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>
Run Code Online (Sandbox Code Playgroud)

2)创建batch默认命名空间(with 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>
Run Code Online (Sandbox Code Playgroud)