spring 解析配置文件问题

问题描述

2014-02-25 16:39:36.068 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2] WARN  c.m.v.resourcepool.BasicResourcePool [U][] - com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@14ca1a93 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 
java.sql.SQLException: Access denied for user 'root'@'10.127.130.163' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) ~[mysql-connector-java-5.1.15.jar:na]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3603) ~[mysql-connector-java-5.1.15.jar:na]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535) ~[mysql-connector-java- 

测试环境出现问题,提示c3p0数据源获取数据库连接被拒绝。并且还出现的是root这个用户,可是我配置文件里面填的是fg_user。

原因

<context:property-placeholder location="classpath*:conf/conf_a.properties"/>

我使用的是spring 3.1.0版本。而spring在3.0版本之后,使用该上述配置会默认使用PropertySourcesPlaceholderConfigurer,该类查找变量的范围是先从spirng 的 Environment中查找,然后才会去自定义的配置文件中找,而我占位符使用的是${user},会优先去取linux当前用户,也就是root。至此,找到原因。

// As of Spring 3.1, the default value of system-properties-mode has changed from
        // 'FALLBACK' to 'ENVIRONMENT'. This latter value indicates that resolution of
        // placeholders against system properties is a function of the Environment and
        // its current set of PropertySources
        if (element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB).equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) {
            return PropertySourcesPlaceholderConfigurer.class;
        }

        // the user has explicitly specified a value for system-properties-mode. Revert
        // to PropertyPlaceholderConfigurer to ensure backward compatibility.
        return PropertyPlaceholderConfigurer.class;
原文地址:https://www.cnblogs.com/beiyeren/p/3571322.html