我是靠谱客的博主 温婉牛排,这篇文章主要介绍Spring学习记录(三),现在分享给大家,希望可以做个参考。

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

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

db,properties,如下:

复制代码
1
2
3
4
user=root password=root driverclass=com.mysql.jdbc.Driver jdbcurl=jdbc:mysql:///test

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


然后我们看xml文件:

复制代码
1
2
3
4
5
6
7
8
<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函数测试:

复制代码
1
2
3
4
5
6
7
8
9
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); }

结果:

复制代码
1
2
信息: 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

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

最后

以上就是温婉牛排最近收集整理的关于Spring学习记录(三)的全部内容,更多相关Spring学习记录(三)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部