spring 使用外部属性文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cygcsdn/article/details/79253321

最近在学 spring 时遇到一个小问题:在配置文件中通过外部属性文件连接数据库时,因为属性的名字取的有问题一直出错.


 原属性文件:

user=root
password=741623
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///Test?useSSL=false


原配置文件部分代码:

<!-- 导入属性文件 -->
   <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>


错误提示:

java.sql.SQLException: Access denied for user 'hk'@'localhost' (using password: YES)

错误是一大堆,意思是我的配置文件有问题.

解决办法  :修改属性文件的属性名称.


新属性文件:

jdbc.user=root
jdbc.password=741623
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///Test?useSSL=false


新配置文件相关代码:

   <!-- 导入属性文件 -->
   <context:property-placeholder location="classpath:db.properties"/>

   <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>

   </bean>


  运行结果:

二月 04, 2018 4:38:15 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@685f4c2e: startup date [Sun Feb 04 16:38:15 CST 2018]; root of context hierarchy
二月 04, 2018 4:38:15 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-properties.xml]
二月 04, 2018 4:38:15 下午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [db.properties]
二月 04, 2018 4:38:15 下午 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
二月 04, 2018 4:38:15 下午 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
二月 04, 2018 4:38:16 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: 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 -> z8kfsx9tqmkr3qvk7tfu|6737fd8f, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> z8kfsx9tqmkr3qvk7tfu|6737fd8f, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql:///Test?useSSL=false, 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, testConnectionOn
com.mchange.v2.c3p0.impl.NewProxyConnection@4ae82894

运行成功!!!


注:可能不是每个人都有我这个问题,但我的确是遇到了.

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页