1. Use XmlBeanFactory to read the spring configuration file:
Resource resource=new ClassPathResource("conf/spring/applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(resource); com.geostar.query.beans.DataSource ds=(com.geostar.query.beans.DataSource)factory.getBean("ds");System.out.println(ds.getDriverClassName());
Console:
Log4j: WARN No such property [maxBackupIndex] in org. apache. log4j. DailyRollingFileAppender.
Log4j: WARN No such property [maxFileSize] in org. apache. log4j. DailyRollingFileAppender.
$ {Google. database. driverClassName}
------ Below is my spring configuration section:
<Bean id = "ds" class = "com. geostar. query. beans. DataSource">
<Property name = "driverClassName">
<Value >$ {google. database. driverClassName} </value>
</Property>
<Property name = "url">
<Value >$ {Google. database. url} </value>
</Property>
<Property name = "username">
<Value >$ {Google. database. Username} </value>
</Property>
<Property name = "password">
<Value >$ {Google. database. Password} </value>
</Property>
<Property name = "tableName">
<Value >$ {google. database. tablename} </value>
</Property>
<Property name = "fieldName">
<Value >$ {google. database. fieldname} </value>
</Property>
<Property name = "orderFieldName">
<Value >$ {google. database. orderfieldname} </value>
</Property>
</Bean>
2. Use ApplicationContext
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:conf/spring/applicationContext.xml"); DataSource ds=(DataSource)factory.getBean("ds"); System.out.println(ds.getDriverClassName());
Console:
------------------------------------------ Spring ioc container initialization ----------------------
Oracle. jdbc. driver. OracleDriver