无法加载指定的别名类

时间:2017-12-02 12:48:40

标签: java xml spring spring-batch

我正在尝试开发一个从XML文件读取并写入MySQL数据库的弹簧批处理。我的context-config xml有问题。

这是我的job-config.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="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-3.2.xsd 
      http://www.springframework.org/schema/util     
      http://www.springframework.org/schema/util/spring-util-3.0.xsd ">

    <import resource="/batch/batch-context.xml" />

    <bean id="itemProcessor" class="com.freesoft.batch.processor.CustomItemProcessor" />

    <!-- <bean id="tasklet" class="com.freesoft.batch.MyTasklet" /> -->

    <bean id="xmlItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
        <property name="fragmentRootElementName" value="tutorial" />
        <property name="resource" value="classpath:resources/inputfile/tutorial.xml" />
        <property name="unmarshaller" ref="customUnMarshaller" />
    </bean>

    <bean id="customUnMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
        <property name="aliases">
            <util:map id="aliases">
                <entry key="tutorial" value="Tutorial" />
            </util:map>
        </property>
    </bean>

    <bean id="mySqlItemWriter"
        class="org.springframework.batch.item.database.JdbcBatchItemWriter">
        <property name="dataSource" ref="dataSource" />
        <property name="sql">
            <value>
                <![CDATA[insert into xdvl.tutorials (tutorial_id, tutorial_author, tutorial_title, 
                    submission_date, tutorial_icon, tutorial_description) 
                    values (:tutorial_id, :tutorial_author, :tutorial_title, :submission_date, 
                    :tutorial_icon, :tutorial_description);]]>
            </value>
        </property>
        <property name="itemSqlParameterSourceProvider">
            <bean
                class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
        </property>
    </bean>

    <batch:job id="helloWorldJob">
        <batch:step id="step1">
            <batch:tasklet>
                <batch:chunk commit-interval="1" reader="xmlItemReader"
                    writer="mySqlItemWriter" processor="itemProcessor" />
            </batch:tasklet>
        </batch:step>
    </batch:job>

</beans> 

这是我的例外:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xmlItemReader' defined in class path resource [resources/job-config.xml]: Cannot resolve reference to bean 'customUnMarshaller' while setting bean property 'unmarshaller'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customUnMarshaller' defined in class path resource [resources/job-config.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Failed to load specified alias class
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1531)
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customUnMarshaller' defined in class path resource [resources/job-config.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Failed to load specified alias class
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)

1 个答案:

答案 0 :(得分:0)

好的,对不起,我发现了问题所在。 问题是在customUnMarshaller bean中我没有为Tutorial类指定包前缀:)

现在,我的customUnMarshaller bean看起来像这样:

    <bean id="customUnMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
    <property name="aliases">
        <util:map id="aliases">
            <entry key="tutorial" value="com.freesoft.batch.model.Tutorial" />
        </util:map>
    </property>
</bean>