spring batch - how to reference bean properties in XML -


i'm developing several spring batch applications create flat files mapped result set objects large numbers of columns.

coding the field extactor's "names" property , line aggregators's "format" property in xml cumbersome , error prone, i've built component hold list of fields , has public method return string array of field names , public method return format specifiers string. here simplified examples:

public class field {     private string fieldname;   // e.g., "firstname"     private string format;      // e.g., "%-16s"     // getters/setters omitted here }  @component public abstract class fieldlist extends arraylist {     public fieldlist () {     }     public string[] getfieldnamearray() {         arraylist<string> names = new arraylist<string>();         (int = 0; < this.size(); i++) {             names.add(this.get(i).getfieldname());         }         return (string[]) names.toarray();      }     public string getformatstrings() {         stringbuffer sb = new stringbuffer ();         (int = 0; < this.size(); i++) {             sb.appeng(this.get(i).getformat());         }         return (string[]) names.toarray();      } }  @component public class employeefieldlist extends fieldlist {     public employeefieldlist () {         super();         this.add(new field("empid",    "%-8s"));         this.add(new field("firstname","%-16s"));         // etc., etc. classes have 50+ fields      } } 

in xml configuration have:

<bean id="employeefieldlist" class="com.stuff.employeefieldlist" />  <bean id="employeefileitemwriter"      class="o.s.b.item.file.flatfileitemwriter" >     <property  name="resource" value="file:${strrunfilename}" />      <property  name="lineaggregator">         <bean class="o.s.b.item.file.transform.formatterlineaggregator"              <property name="fieldextractor">                 <bean               class="o.s.b.item.file.transform.beanwrapperfieldextractor"                     <property name="names" ref="fieldnamearray" />                 </bean>             </property >             <property name="format" ref="formatstrings" />         </bean>                                  </property > </bean> 

the component conforms javabean api, once define component in xml should able reference public members well, right?

however doesn't work. in log see this:

error creating bean name  'o.s.b.item.file.transform.beanwrapperfieldextractor#60cbf9bd'   defined in class path resource [spring/module-context.xml]:   cannot resolve reference bean 'fieldnamearray' while setting bean  property 'names';   nested exception o.s.b.factory.nosuchbeandefinitionexception:   **no bean named 'fieldnamearray' defined** 

i've tried applying several notational conventions still error, appears references may made beans, not public properties. or missing something?

correct. spring's ref attribute refers bean id.

that being said, i'd recommend different approach. instead of creating component returns string of names, i'd recommend creating factorybean fieldextractor. can used encapsulate creation of beanwrapperfieldextractor configured want.

an example this:

public class complexbeanwrapperfieldextractorfactory implements factorybean<fieldextractor> {   public fieldextractor getobject() {     // put logic here generate string     // put logic here create instance of beanwrapperfieldextractor configured need     // return instance created.   } } 

with above code, can configure reader follows:

<bean id="fieldextractorfactory" class="complexbeanwrapperfieldextractorfactory/>  <bean id="employeefileitemwriter"      class="o.s.b.item.file.flatfileitemwriter" >     <property  name="resource" value="file:${strrunfilename}" />      <property  name="lineaggregator">         <bean class="o.s.b.item.file.transform.formatterlineaggregator"              <property name="fieldextractor" ref="fieldextractorfactory"/>             <property name="format" ref="formatstrings" />         </bean>                                  </property > </bean> 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -