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 1988
情深已故
情深已故 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 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 implements ItemStreamReader {
    
    /** Registered ItemStreamReaders. */
    private List> cursorItemReaders;
    /** Mandatory Unifying Mapper Implementation. */
    private UnifyingItemsMapper 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:

      
                    
          
    

    here is the implementation of the UnifyingItemsMapper you are using:

     public class DefaultUnifyingStringItemsMapper implements UnifyingItemsMapper {
    
        /** {@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 and configure it for your CompositeCursorItemReader#unifyingMapper

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

     
            
                
                    
                
            
    
            
                
                    
                
            
        
    

提交回复
热议问题