利用反射,根据文件配置,进行对象的实例化操作.
如下是做Migrate CSV data to DB2 时使用到得spring.xml 配置文件:
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd" >


<!--Configure CSVMigrationManager-->
<bean id="csvMigrationManager" class="com.ibm.pw.pps.CSVMigrationManager">
<!--This is for filtering csv files in a directory. This is required-->
<property name="fileFilter">
<bean class="com.ibm.pw.pps.filefilter.CSVFileFilter"/>
</property>
<!--If this is true, the table definition file for each csv file will be generated. The name of the definition file is the csv file's name appended by ".def.xml". This is optional, default to false-->
<property name="generateTableDefinitionFile" value="true"/>
<!--The path of the input csv file. This is required-->
<property name="inputPath" value="test_files/in.csv"/>
<!--The name of the Migrator instance to use to perform migration. The command line can use "-migrator" option to override this property. This is required-->
<property name="selectedMigratorName" value="xml"/>
<!--The parameters that are passed into the selected Migrator instance's migrate() method.
Some migrator might need this. For example, CSVToXMLMigrator needs the output path. This is optional, default to an empty map.-->
<property name="migratorParameters">
<map>
<entry key="outputPath" value="test_files/in.csv.xml"/>
</map>
</property>
<!--This is a map of one or more Migrator instances configured to be used.
Only one of them(named by selectedMigratorName) is used for a call on the no-arg migrate().
But caller can specify other Migrator without changing selectedMigratorName by calling the overloaded migrate() method. This is required-->
<property name="migrators">
<!--The key of the map is the unique identifier of a Migrator. Both 'selectedMigratorName' and the '-migrator' option of the command line should use this key to refer to Migrator instances.-->
<map>
<entry key="db2">
<!--This bean must be configured if migration to a database needs to be supported-->
<bean class="com.ibm.pw.pps.migratorimpl.CSVToDB2Migrator">
<!--This must be configured so that CSVToDB2Migrator can get the selected data source. This is required-->
<property name="dataSourceFactory" ref="dataSoutceFactory"/>
<!--A map of Converter instances can be configured, which are used for converting the string field in the csv file into other types. This is optional because ConvertUtils has some default converters. However if there is date value in a csv file, we must configure a DateLocaleConverter to convert string into java.util.Date-->
<property name="converters">
<map>
<entry key="java.util.Date">
<!--This is used for convert string into java.util.Date. Please refer to Apache Commons BeanUtils's doc(http://commons.apache.org/beanutils/commons-beanutils-1.7.0/docs/api/org/apache/commons/beanutils/locale/converters/DateLocaleConverter.html) for its ctor parameters-->
<bean class="org.apache.commons.beanutils.locale.converters.DateLocaleConverter">
<constructor-arg index="0">
<bean class="java.util.Locale">
<constructor-arg index="0" value="en"/>
</bean>
</constructor-arg>
<constructor-arg index="1" value="yyyy-MM-dd"/>
<constructor-arg index="2" value="true"/>
</bean>
</entry>
</map>
</property>
</bean>
</entry>
<entry key="xml">
<!--This bean must be configured if migration to a XML file needs to be supported-->
<bean class="com.ibm.pw.pps.migratorimpl.CSVToXMLMigrator"/>
</entry>
</map>
</property>
<!--The CSVParser instance for parsing a csv file. This is required-->
<property name="parser">
<bean class="com.ibm.pw.pps.csvparser.CSVParserImpl"/>
</property>
<!--The TableDefinitionFactory instance for creating a TableDefinition from the first row of a csv file. This is required-->
<property name="tableDefinitionFactory">
<bean class="com.ibm.pw.pps.tabledefinitionfactoryimpl.TableDefinitionFactoryImpl">
<!--This must be configured so that TableDefinitionFactoryImpl can get the selected data source. This is required-->
<property name="dataSourceFactory" ref="dataSoutceFactory"/>
</bean>
</property>
<!--The TableDefinitionWriter used for writing a table definition file for a csv file. This is required-->
<property name="tableDefinitionWriter">
<bean class="com.ibm.pw.pps.tabledefinitionwriter.XMLTableDefinitionWriter"/>
</property>
</bean>
</beans>
重点关注Manager 类的实例化,<bean> csvMigrationManager 下配置了com.ibm.pw.pps.CSVMigrationManager一系列字段的属性.
源码未曾研究过,猜想应该是利用反射直接设置字段,即使它是private的.
至于使用,如下:
// Create an ApplicationContext
ApplicationContext context = new FileSystemXmlApplicationContext("test_files"
+ System.getProperty("file.separator") + "spring.xml");

// Get the Spring-configured CSVMigrationManager
CSVMigrationManager manager = (CSVMigrationManager) context.getBean("csvMigrationManager");