Spring学习记录(三)

我们在spring使用中,有时候需要用到外部文件,如数据库配置,这是好,我们来学习下使用外部属性文件:

首先,我们在src下新建一个db.properties文件,它的里面保存我们的数据库设置,然后我们应该在xml文件读取它,

db,properties,如下:

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

我们的数据库配置,这时候急着引入两个jar包,mysql与c3p0,截图:

Spring学习记录(三)_第1张图片

然后我们看xml文件:









配置好后,我们在main函数测试:

public static void main(String[] args) {
		ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
//		Person person = (Person) ac.getBean("person1");
//		System.out.println(person);
		
		DataSource dataSource =(DataSource) ac.getBean("dataSource");
		System.out.println(dataSource);
	}

结果:

信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1b610709u4c9vgvx36tix|1b68ddbd, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1b610709u4c9vgvx36tix|1b68ddbd, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql:///test, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1b610709u4c9vgvx36tix|1b68ddbd, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, fac

结果太长,我们贴出来一部分,结果是已经成功连接。

你可能感兴趣的:(JAVA,JAVA框架,Spring)