2017-01-06 20 views
1

Я разрабатываю пример Spring Batch - MongoDB to XML. В этом примере, когда я запускаю основной метод, я вижу, что ошибка ниже. Пожалуйста, обратитесь к приведенной ниже ошибке. Я попытался найти решение в Интернете, но я пока не нашел ничего полезного в Интернете.Spring Batch - MongoDB to XML - Caused by: java.lang.IllegalStateException: Требуется тип для преобразования входных данных

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'step1': Cannot resolve reference to bean 'mongodbItemReader' while setting bean property 'itemReader'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongodbItemReader' defined in class path resource [job-report.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: A type to convert the input into is required. 
    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:1492) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1237) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:552) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:740) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93) 
    at com.mkyong.App.main(App.java:15) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongodbItemReader' defined in class path resource [job-report.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: A type to convert the input into is required. 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1589) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 
    ... 15 more 
Caused by: java.lang.IllegalStateException: A type to convert the input into is required. 
    at org.springframework.util.Assert.state(Assert.java:392) 
    at org.springframework.batch.item.data.MongoItemReader.afterPropertiesSet(MongoItemReader.java:200) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1648) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1585) 
    ... 22 more 

database.xml

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

     <!-- connect to mongodb --> 
    <mongo:mongo host="127.0.0.1" port="27017" /> 
    <mongo:db-factory dbname="yourdb" /> 

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
     <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> 
    </bean> 

</beans> 

context.xml

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

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

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


    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> 
     <property name="jobRepository" ref="jobRepository" /> 
    </bean> 
</beans> 

рабочих мест report.xml

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

    <batch:job id="reportJob"> 
     <batch:step id="step1"> 
      <batch:tasklet> 
       <batch:chunk reader="mongodbItemReader" writer="xmlItemWriter" commit-interval="1"> 
       </batch:chunk> 
      </batch:tasklet> 
     </batch:step> 
    </batch:job> 

    <bean id="mongodbItemReader" class="org.springframework.batch.item.data.MongoItemReader"> 
     <property name="template" ref="mongoTemplate" /> 
     <property name="collection" value="report" /> 
    </bean> 

    <bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter"> 
     <property name="resource" value="classpath:xml/report.xml" /> 
     <property name="marshaller" ref="reportMarshaller" /> 
     <property name="rootTagName" value="record" /> 
    </bean> 

    <!-- ==== Solution-1 ==== --> 
    <bean id="reportMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
     <property name="classesToBeBound"> 
      <value>com.mkyong.Report</value> 
     </property> 
    </bean> 

    <!-- ==== Solution-2 ==== --> 
    <!-- <bean id="reportMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller"> 
     <property name="aliases"> 
      <util:map id="aliases"> 
       <entry key="record" value="com.mkyong.Report" /> 
      </util:map> 
     </property> 
     <property name="converters"> 
      <array> 
       <ref bean="reportConverter" /> 
      </array> 
     </property> 
    </bean> --> 

    <bean id="reportConverter" class="com.mkyong.ReportConverter" /> 
</beans> 

Repoer.java

@XmlRootElement(name = "record") 
public class Report { 

    private int id; 
    private Date date; 
    private long impression; 
    private int clicks; 
    private BigDecimal earning; 

    @XmlAttribute(name = "id") 
    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    @XmlElement(name = "date") 
    public Date getDate() { 
     return date; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

    @XmlElement(name = "impression") 
    public long getImpression() { 
     return impression; 
    } 

    public void setImpression(long impression) { 
     this.impression = impression; 
    } 

    @XmlElement(name = "clicks") 
    public int getClicks() { 
     return clicks; 
    } 

    public void setClicks(int clicks) { 
     this.clicks = clicks; 
    } 

    @XmlElement(name = "earning") 
    public BigDecimal getEarning() { 
     return earning; 
    } 

    public void setEarning(BigDecimal earning) { 
     this.earning = earning; 
    } 
} 

App.java

public class App { 
    public static void main(String[] args) { 

     String[] springConfig = { "database.xml", "context.xml", "job-report.xml" }; 

     ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); 

     JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); 
     Job job = (Job) context.getBean("reportJob"); 

     try { 
      JobExecution execution = jobLauncher.run(job, new JobParameters()); 
      System.out.println("Exit Status : " + execution.getStatus()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     System.out.println("Done"); 
    } 
} 

Не могли бы вы помочь в этом?

ответ

0

Спасибо @ Серхио, это была большая помощь от вас, но ниже конфигурационных работ.

<bean id="mongodbItemReader" class="org.springframework.batch.item.data.MongoItemReader"> 
     <property name="template" ref="mongoTemplate" /> 
     <property name="collection" value="report" /> 
     <property name="targetType" value="com.mkyong.model.Report" /> 
     <property name="query" value="{'_id':{$gt:0} }" /> 
     <property name="sort"> 
      <util:map> 
       <entry key="id" value="#{T(org.springframework.data.domain.Sort.Direction).ASC}" /> 
      </util:map> 
     </property> 
    </bean> 

Единственная проблема, которую я вижу, ничего не записывает в файл XML. Я задам отдельный вопрос по этому вопросу.

Примечание: Основной причиной этой ошибки является то, что вы должны установить <property name="targetType" value="com.mkyong.model.Report" /> это свойство правильно.

Вы также можете обратиться: http://www.mkyong.com/spring-batch/how-to-convert-date-in-beanwrapperfieldsetmapper/ как другое решение.

0

Как указывает ошибка, вам необходимо установить свойство type для вашего компонента "mongodbItemReader".

См. MongoItemReader javadoc.

Пример:

<bean id="mongodbItemReader" class="org.springframework.batch.item.data.MongoItemReader"> 
    <property name="template" ref="mongoTemplate" /> 
    <property name="collection" value="report" /> 
    <property name="targetType" value="Report.class" /> 
</bean> 

Вы, возможно, потребуется указать имя пакета в значение (я не пробовал).

+0

Нет, не работает. Я получаю другую ошибку. Вызвано: 'org.springframework.beans.TypeMismatchException: Не удалось преобразовать значение свойства type [java.lang.String] в требуемый тип [java.lang.Class] для свойства 'targetType'; inested exception is java.lang.IllegalArgumentException: Не удается найти класс [com.mkyong.model.Report.class] ' –