19.4 Property Placeholder Configuration 6.0.0

(Quick Reference) # 19.4 Property Placeholder Configuration Version: 6.0.0 ## 19.4 Property Placeholder Configuration Grails supports the notion of property placeholder configuration through an extended version of Spring’s PropertyPlaceholderConfigurer. Settings defined in either ConfigSlurper scripts or Java properties files can be used as placeholder values for Spring configuration in `grails-app/conf/spring/resources.xml` and `grails-app/conf/spring/resources.groovy`. For example given the following entries in `grails-app/conf/application.groovy` (or an externalized config):
    database.driver="com.mysql.jdbc.Driver"
    database.dbname="mysql:mydb"
You can then specify placeholders in `resources.xml` as follows using the familiar ${..} syntax:
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>${database.driver}</value>
        </property>
        <property name="url">
            <value>jdbc:${database.dbname}</value>
        </property>
     </bean>
To specify placeholders in `resources.groovy` you need to use single quotes:
    dataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) {
        driverClassName = '${database.driver}'
        url = 'jdbc:${database.dbname}'
    }
This sets the property value to a literal string which is later resolved against the config by Spring’s PropertyPlaceholderConfigurer. A better option for `resources.groovy` is to access properties through the `grailsApplication` variable:
    dataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) {
        driverClassName = grailsApplication.config.database.driver
        url = "jdbc\:${grailsApplication.config.database.dbname}"
    }
Using this approach will keep the types as defined in your config. | Quick Reference (hide)