How can I get properties's config in DataSource.groovy?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I get properties's config in DataSource.groovy?

xunitc
Hi.

I want to get the properties's config in DataSource.groovy. I try the Holders, but it is null in DataSource.groovy. I search and find some posts, but they are too old, I try but it can not work.

How to get the config in DataSource.groovy please?

Some files here:
Grails version is 2.2.1.

app.properties is:
mymongo=mydb

DataSource.groovy is:
import grails.util.Holders;
grails {
        mongo {
                host = "localhost"
                port = 27017
                // this line can not work.
                databaseName = Holders.config.mymongo
        }
}
Xunitc
Reply | Threaded
Open this post in threaded view
|

Re: How can I get properties's config in DataSource.groovy?

Ian Roberts
On 20/05/2013 12:11, xunitc wrote:

> app.properties is:
> mymongo=mydb
>
> DataSource.groovy is:
> import grails.util.Holders;
> grails {
> mongo {
> host = "localhost"
> port = 27017
> // this line can not work.
> databaseName = Holders.config.mymongo
> }
> }

I'm assuming here that app.properties is declared as a
grails.config.locations external config file.  You're correct that you
can't refer to the config via Holders at this stage (because
DataSource.groovy is processed before the external config files have
been loaded), however it is possible to set or override DataSource
entries directly in the external file, i.e.

// DataSource.groovy

grails {
        mongo {
                host = "localhost"
                port = 27017
        }
}

// app.properties
grails.mongo.databaseName=mydb

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: How can I get properties's config in DataSource.groovy?

xunitc
Hi.
Thanks a lot Ian Roberts!
I try it and it works now.

And other question please:
If I want the properties server different enviroments, how can I do it
please? Like:

app.properties:
grails.mongo.databaseName=mydb
#development.grails.mongo.databaseName=mydevdb
#production.grails.mongo.databaseName=myprodb

DataSource.groovy:
    grails {
        mongo {
            host = "localhost"
            port = 27017
        }
    }
/*
environments {
    development {
        //grails.mongo.databaseName=mydevdb
    }
    production {
        //grails.mongo.databaseName=myprodb
    }
}
*/
Sorry, I do not konw how to write the different environments in
DataSource.groovy, the above is just a form.

2013/5/20 Ian Roberts <[hidden email]>:

> On 20/05/2013 12:11, xunitc wrote:
>> app.properties is:
>> mymongo=mydb
>>
>> DataSource.groovy is:
>> import grails.util.Holders;
>> grails {
>>       mongo {
>>               host = "localhost"
>>               port = 27017
>>               // this line can not work.
>>               databaseName = Holders.config.mymongo
>>       }
>> }
>
> I'm assuming here that app.properties is declared as a
> grails.config.locations external config file.  You're correct that you
> can't refer to the config via Holders at this stage (because
> DataSource.groovy is processed before the external config files have
> been loaded), however it is possible to set or override DataSource
> entries directly in the external file, i.e.
>
> // DataSource.groovy
>
> grails {
>         mongo {
>                 host = "localhost"
>                 port = 27017
>         }
> }
>
> // app.properties
> grails.mongo.databaseName=mydb
>
> Ian
>
> --
> Ian Roberts               | Department of Computer Science
> [hidden email]  | University of Sheffield, UK
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>



--
Xunitc.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Xunitc
Reply | Threaded
Open this post in threaded view
|

Re: How can I get properties's config in DataSource.groovy?

Ian Roberts
On 20/05/2013 13:20, xunitc wrote:

> /*
> environments {
>     development {
>         //grails.mongo.databaseName=mydevdb
>     }
>     production {
>         //grails.mongo.databaseName=myprodb
>     }
> }
> */
> Sorry, I do not konw how to write the different environments in
> DataSource.groovy, the above is just a form.

That is the correct way to do environments in DataSource.groovy - it's
exactly the same as in Config.groovy.  You can do the same with external
.groovy config files, but I don't think external .properties files can
do it.  But you could say something like

grails.config.locations = ["file:app.properties",
"file:app-${grails.util.Environment.current.name}.properties"]

and then have separate app-development.properties,
app-production.properties, etc.

Ian

--
Ian Roberts               | Department of Computer Science
[hidden email]  | University of Sheffield, UK

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: How can I get properties's config in DataSource.groovy?

xunitc
Hi.
Thank you so much Ian Roberts!
With your help it works now. And I change the grails.serverURL config
as your way, it is so nice!

Thanks again!

2013/5/20 Ian Roberts <[hidden email]>:

> On 20/05/2013 13:20, xunitc wrote:
>> /*
>> environments {
>>     development {
>>         //grails.mongo.databaseName=mydevdb
>>     }
>>     production {
>>         //grails.mongo.databaseName=myprodb
>>     }
>> }
>> */
>> Sorry, I do not konw how to write the different environments in
>> DataSource.groovy, the above is just a form.
>
> That is the correct way to do environments in DataSource.groovy - it's
> exactly the same as in Config.groovy.  You can do the same with external
> .groovy config files, but I don't think external .properties files can
> do it.  But you could say something like
>
> grails.config.locations = ["file:app.properties",
> "file:app-${grails.util.Environment.current.name}.properties"]
>
> and then have separate app-development.properties,
> app-production.properties, etc.
>
> Ian
>
> --
> Ian Roberts               | Department of Computer Science
> [hidden email]  | University of Sheffield, UK
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>



--
Xunitc.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Xunitc