在spring applicationContext.xml中访问系统属性时出错

时间:2018-08-02 10:53:21

标签: java spring

我想将我的db文件位置设置为系统变量的一部分,该变量将在启动应用程序时提供。 我想访问applicationContext.xml中试图在#{systemProperties['db.properties']}中使用此系统属性的applicationContext.xml.

我已经通过以下两种方式定义了propertyPlaceholderConfigurer bean,它通过两种方式为属性“位置”指定值:

案例1#:

<bean id="propertyPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="searchSystemEnvironment" value="true"/>
        <property name="locations">

             <value>file://"#{systemProperties['db.properties']}"</value>
        </property>
        <property name="ignoreResourceNotFound" value="true" />
         <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

数据源bean#:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
        destroy-method="close"> 
     <property name="driverClassName" value="org.postgresql.Driver"/> 
     <property name="url" value="${db.url}"/>
     <property name="username" value="${db.user}"/>
     <property name="password" value="${db.pwd}"/>
</bean> 

案例2#:

<bean id="propertyPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="searchSystemEnvironment" value="true"/>
        <property name="locations">

            <value>file://${DB_CONF}/test/db.properties</value>
        </property>
        <property name="ignoreResourceNotFound" value="true" />
         <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

如果我像情况2那样使用属性位置的值,而不是 “#{systemProperties ['db.properties']}”应用程序运行正常。 在情况1中,我尝试从与系统属性相同的位置访问相同的属性文件,但是应用程序无法正常工作,出现以下错误:

堆栈跟踪

Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:235)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171)
    at org.hibernate.internal.SessionImpl.connection(SessionImpl.java:450)
    at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:450)
    ... 133 more
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'org.postgresql.Driver' for connect URL '${db.url}'
    at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
    ... 136 more
Caused by: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:315)
    at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)

2 个答案:

答案 0 :(得分:0)

在这里找到如何使用xml读取属性文件

<bean id="propertyBean"
    class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
             <!-- This will find you file inside system classpath -->
            <value>classpath:application.properties</value>
            <!-- or you pass the whole path -->
            <!--<value>file:///opt/application.properties</value> -->
        </list>
    </property>
</bean> 

答案 1 :(得分:0)

我可以解决该错误。罪魁祸首是双引号。 使用file://#{systemProperties ['db.properties']}“代替file://”#{systemProperties ['db.properties']}“”解决了该问题。