属性文件加载以替换spring批处理XML中的值

时间:2014-02-03 11:00:34

标签: java xml spring properties spring-batch

因为有一天我正在处理一个问题。为了使我的代码更易于维护,我想在一个属性文件(My_Project / src / main / resources / spring / batch / config / batch.properties)中设置Spring Batch中我需要的所有常量。

我在网上搜索了几个例子但没什么用。为此,我用$ {constantNameInPorpertiesFile}替换我的常量

我的context.xml MY_PROJECT / SRC /主/资源/弹簧/批次/配置/ context.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">

 <!-- stored job-meta in memory-->
    <bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>

    <bean id="transactionManager"
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

    <bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>
    <context:property-placeholder location="classpath:spring/batch/config/batch.properties"/>
</beans>

在这个context.xml中,我希望如此  “”将替换导入context.xml的遗嘱中的常量。

例如这个春季批处理文件(My_Project / src / main / resources / spring / batch / jobs / CreateSpecificationFileJob.xml):

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:property-placeholder location="classpath:spring/batch/config/batch.properties"/>

<import resource="../config/context.xml"/>  

<!-- Flow for the V4 specification file creation -->
<flow id="createSpecificationFileFlow" xmlns="http://www.springframework.org/schema/batch">

    <step id="formRecognitionPartitionerStep">
        <partition partitioner="formRecognitionPartitioner">
            <handler grid-size="1" task-executor="syncTaskExecutor" />
            <!-- partitioner will run this job, with different stepExecutionContext values.      -->
            <step>
                <flow parent="createAndCompleteSpecificationFile"/>
            </step>
        </partition>
    </step>
</flow>

<!-- sub-Flow to make all process on each form -->  
<flow id="createAndCompleteSpecificationFile" xmlns="http://www.springframework.org/schema/batch">
    <step id="createSpecificationFile" next="sheetRecognitionPartitionerStep">
        <tasklet ref="createSpecificationFileTasklet"/>
    </step>

    <step id="sheetRecognitionPartitionerStep">
        <partition partitioner="sheetRecognitionPartitioner">
            <handler grid-size="1" task-executor="syncTaskExecutor" />
            <!--partitioner will run this job, with different stepExecutionContext values. -->
            <step>
                <flow parent="completeSheet"/>
            </step>
        </partition>
    </step>
</flow>

<!-- sub-Flow to make all process on each sheet of a form -->   
<flow id="completeSheet" xmlns="http://www.springframework.org/schema/batch">
    <step id="gridRecognition" next="labelRecognition">
        <tasklet ref="gridRecognitionTasklet"/>
    </step>
    <step id="labelRecognition" next="putDataInV4SpecificationFile">
        <tasklet ref="labelRecognitionTasklet"/>
    </step>
    <step id="putDataInV4SpecificationFile" next="constantManagement">
        <tasklet ref="putDataInV4SpecificationFileTasklet"/>
    </step>
    <step id="constantManagement" next="fileHeaderAndFooter">
        <tasklet ref="constantManagementTasklet"/>
    </step>
    <step id="fileHeaderAndFooter" next="sheetLayoutForPrinting">
        <tasklet ref="fileHeaderAndFooterTasklet"/>
    </step>
    <step id="sheetLayoutForPrinting">  
        <tasklet ref="sheetLayoutForPrintingTasklet"/>
    </step> 
</flow>


<!-- List of tasklet -->
<bean id="createSpecificationFileTasklet" class="com.sopra.ner.specification.excel.write.CreateSpecificationFileTasklet" scope="step" >
    <property name="${partitionner.list_of_sheet}" value="#{stepExecutionContext['${partitionner.list_of_sheet}']}" />
</bean>
<bean id="gridRecognitionTasklet" class="com.sopra.ner.specification.excel.read.GridRecognitionTasklet" scope="step">
    <property name="${partitionner.sheet}" value="#{stepExecutionContext['${partitionner.sheet}']}" />
</bean>
<bean id="labelRecognitionTasklet" class="com.sopra.ner.specification.excel.read.LabelRecognitionTasklet" scope="step"/>
<bean id="putDataInV4SpecificationFileTasklet" class="com.sopra.ner.specification.excel.write.PutDataInV4SpecificationFileTasklet" scope="step"/>
<bean id="constantManagementTasklet" class="com.sopra.ner.specification.excel.write.ConstantManagementTasklet" scope="step"/>
<bean id="fileHeaderAndFooterTasklet" class="com.sopra.ner.specification.excel.write.FileHeaderAndFooterTasklet" scope="step"/>
<bean id="sheetLayoutForPrintingTasklet" class="com.sopra.ner.specification.excel.write.SheetLayoutForPrintingTasklet" scope="step"/>

<bean id="syncTaskExecutor" class="org.springframework.core.task.SyncTaskExecutor"/>

<!-- List of partition -->
<bean id="formRecognitionPartitioner" class="com.sopra.ner.specification.excel.FormRecognitionPartitioner" scope="step"/>
<bean id="sheetRecognitionPartitioner" class="com.sopra.ner.specification.excel.SheetRecognitionPartitioner" scope="step">
    <property name="${partitionner.list_of_sheet}" value="#{jobExecutionContext['${partitionner.list_of_sheet}']}" />
</bean>

</beans>

此流程由作业启动。没有任何常数。 错误是: - bean类的无效属性'$ {partitionner.list_of_sheet}'[com.sopra.ner.specification.excel.write.CreateSpecificationFileTasklet] ................

当我在目标文件夹中打开文件时,该值不会被替换。

如果你有任何想法要解决这个问题,我将不胜感激。

0 个答案:

没有答案