2017-01-06 6 views
1

예를 들어 Spring Batch - MongoDB to XML을 개발 중입니다. 이 예제에서 main 메소드를 실행할 때 아래 오류가 발생하는 것을 볼 수 있습니다. 아래 오류로 안내하십시오. 나는 웹상에서 해결책을 찾으려고 노력했지만, 아직 웹에서 도움이되는 것을 찾지 못했다.스프링 일괄 처리 - MongoDB to XML - 원인 : 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

@Sergio에 감사드립니다. 훌륭한 도움을 주셨지만 아래 설정이 작동합니다.

<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

오류가 나타남에 따라 "mongodbItemReader"bean에 "type"속성을 설정해야합니다.

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 : 'java.lang.String'유형의 속성 값을 'targetType'속성의 필수 유형 [java.lang.Class]로 변환하지 못했습니다. 중첩 예외가 java.lang.IllegalArgumentException : 클래스를 찾을 수 없습니다 [com.mkyong.model.Report.class]' –