第一步:
引入context的命名空间和约束路径
//命名空间
xmlns:context="http://www.springframework.org/schema/context"
//约束路径
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
第二部:
写properties配置文件
jdbc.driver=com.mysql.jdbc.Driver;
jdbc.url=jdbc:mysql://localhost:3306/
jdbc.username=root
jdbc.password=root
第三步:
加载外部配置文件及获取配置信息
<!--加载外部文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--获取配置信息-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
第四步测试:
@Test
public void test() throws Exception {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) app.getBean("dataSource");
Connection connection=dataSource.getConnection();
System.out.println(connection);
}