spring--(10)使用外部屬性文件

經常使用的外部屬性文件db.properties。
db.properties文件java

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

xml配置文件mysql

<!-- 導入屬性文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property>
		<property name="driverClass" value="${driverClass}"></property>
		<property name="jdbcUrl" value="${jdbcurl}"></property>
	</bean>

main文件spring

package com.test.autowired;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	@SuppressWarnings("resource")
	public static void main(String[] args) throws SQLException {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextRelationxml.xml");
		
		DataSource dataSource = (DataSource) ctx.getBean("dataSource");
		Connection Connection = dataSource.getConnection();
		System.out.println(Connection);
	}
}
相關文章
相關標籤/搜索