Spring imports external resource files

db.properties

jdbc.user=root
jdbc.password=root
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test

jdbc.initPoolSize=5
jdbc.maxPoolSize=10

 beans.xml

<!-- Import external resource files-->
<context:property-placeholder location="classpath:db.properties"/>
	
<!-- configure data source -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="user" value="${jdbc.user}"></property>
	<property name="password" value="${jdbc.password}"></property>
	<property name="driverClass" value="${jdbc.driverClass}"></property>
	<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
	<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
	<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>

test

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
// test using external properties file
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());

  result

com.mchange.v2.c3p0.impl.NewProxyConnection@32450c11

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326484840&siteId=291194637