////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

Grails supports the notion of property placeholder configuration through an extended version of Spring's {springapi}org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html[PropertyPlaceholderConfigurer].

Settings defined in either {groovyapi}groovy/util/ConfigSlurper.html[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):

[source,groovy]
----
database.driver="com.mysql.jdbc.Driver"
database.dbname="mysql:mydb"
----

You can then specify placeholders in `resources.xml` as follows using the familiar ${..} syntax:

[source,xml]
----
<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:

[source,groovy]
----
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:

[source,groovy]
----
dataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) {
    driverClassName = grailsApplication.config.getProperty('database.driver', String)
    url = "jdbc\:${grailsApplication.config.getProperty('database.dbname', String)}"
}
----

Using this approach will keep the types as defined in your config.
