1、使用XmlBeanFactory读取spring配置文件,可以如下:
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());
控制台:
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}
------下面是我的spring配置部分:
<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、使用ApplicationContext
ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:conf/spring/applicationContext.xml"); DataSource ds=(DataSource)factory.getBean("ds"); System.out.println(ds.getDriverClassName());
控制台:
--------------------------------------------spring ioc容器初始化----------------------
oracle.jdbc.driver.OracleDriver