Bean property is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? Spring Batch

后端 未结 2 1965
情深已故
情深已故 2020-12-18 12:15

I am developing Spring Batch CompositeItemReader & Writer Example. I developed XML file and able to compile code successfully, but when running mu sample co

相关标签:
2条回答
  • 2020-12-18 12:47

    You declare your class Customer as:

    public class Customer implements Serializable{
        private static final long serialVersionUID = 1L;
    
        private Integer customerNumber;
        private String customerName;
        private String contactLastName;
        private String contactFirstName;
        ....
        // setters and getters
    }
    

    All of your fields are private and you have not yet implemented public getter/setter methods. Do that and your issue will be solved.

    0 讨论(0)
  • 2020-12-18 13:04

    Spring is trying to get the property cutomerNumber from the String class :

     Invalid property 'customerNumber' of bean class [java.lang.String]
    

    which is wrong because the String is not supposed to be configured as the item of your job 's step, the Item in this context is supposed to be the Customer.

    your beans are configured correctly except for one bean :compositeItemReader

    The read method of this bean is returning a String instead of a Customer which is because of this :

    public class CompositeCursorItemReader<T> implements ItemStreamReader<T> {
    
    /** Registered ItemStreamReaders. */
    private List<AbstractCursorItemReader<?>> cursorItemReaders;
    /** Mandatory Unifying Mapper Implementation. */
    private UnifyingItemsMapper<T> unifyingMapper;
    
    @Override
    public T read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        // read from all registered readers
        List items = new ArrayList();
        for (AbstractCursorItemReader<?> cursorItemReader : cursorItemReaders) {
            items.add(cursorItemReader.read());
        }
        // delegate to mapper
        return unifyingMapper.mapItems(items);
    }
    }
    

    Its property UnifyingMapper is configured as follow:

      <property name="unifyingMapper">
                    <bean class="com.common.batch.mapper.DefaultUnifyingStringItemsMapper" />
          </property>
    

    here is the implementation of the UnifyingItemsMapper you are using:

     public class DefaultUnifyingStringItemsMapper implements UnifyingItemsMapper<String> {
    
        /** {@inheritDoc} */
        @Override
        public String mapItems(List<?> items) throws Exception {
            if (items != null && items.size() > 0) {
                StringBuilder sb = new StringBuilder();
                for (Object item : items) {
                    if (item != null) {
                        sb.append(item);
                    }
                }
                if (sb.length() > 0) {
                    return sb.toString();
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }
    }
    

    the solution to this is to create a new Class that implements UnifyingItemsMapper<Customer> and configure it for your CompositeCursorItemReader#unifyingMapper

    Or just use your itemReader directly if you don't need a UnifyingItemsMapper:

     <job id="compositeJdbcReaderJob" xmlns="http://www.springframework.org/schema/batch">
            <step id="compositeJdbcReaderStep" next="compositeJdbcReaderStep2">
                <tasklet>
                    <chunk reader="itemReader1" writer="itemWriter" commit-interval="5" />
                </tasklet>
            </step>
    
            <step id="compositeJdbcReaderStep2">
                <tasklet>
                    <chunk reader="itemReader2" writer="itemWriter2" commit-interval="5" />
                </tasklet>
            </step>
        </job>
    
    0 讨论(0)
提交回复
热议问题