java - 无法加载指定的别名类

标签 java xml spring spring-batch

我正在尝试开发一个从 XML 文件读取并写入 MySQL 数据库的 spring-batch。我的上下文配置 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):

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)

最佳答案

好的,抱歉,我已经找到问题所在了。 问题是在 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>

关于java - 无法加载指定的别名类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47608068/

相关文章:

java - 如何将点击监听器添加到 Recycler View 的 subview ?

java - "open"模块的 JDK 编译器失败

java - 在Java 1.4中使用Apache httpclient读取URL内容

java - 将 TreeSet 转换为 ArrayList?

c# - 在 C# 中使用 xdocument 从 xml 文件中提取特定数据

xml - XPath 1.0 可以进行通用量化 ("for all") 吗?

xml - 如何设置属性以及如何在 nant 中获取它

java - Log4j警告: no appenders found even if it is there

java spring命名查询和属性文件

java - Spring:将属性文件注入(inject) map