Using a Service from a static Domain method?

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

Using a Service from a static Domain method?

strayph
Hello, all,

I have a Page Domain model, and it uses a PageInfoService to get information about a Page.
I now want to be able to create Pages based on information from the Service.
Since I was accessing other Pages via static domain methods, like Page.find(), I thought I would create a new Page.retrieveByInfoType().

However, the PageInfoService is injected into the objects, not the class, or at least, I haven't figured out how to access it.  

Am I missing something?

Strayph
Reply | Threaded
Open this post in threaded view
|

Re: Using a Service from a static Domain method?

felipecao
You're right, injections are on instance level, they're not present on a static context.
To achieve what you want, you should use ApplicationHolder to retrieve a bean from Spring context:

PageInfoService piService = ApplicationHolder.application.mainContext.getBean('pageInfoService ')

On Fri, Jun 10, 2011 at 5:08 PM, strayph <[hidden email]> wrote:
Hello, all,

I have a Page Domain model, and it uses a PageInfoService to get information
about a Page.
I now want to be able to create Pages based on information from the Service.
Since I was accessing other Pages via static domain methods, like
Page.find(), I thought I would create a new Page.retrieveByInfoType().

However, the PageInfoService is injected into the objects, not the class, or
at least, I haven't figured out how to access it.

Am I missing something?

Strayph

--
View this message in context: http://grails.1312388.n4.nabble.com/Using-a-Service-from-a-static-Domain-method-tp3589300p3589300.html
Sent from the Grails - user mailing list archive at Nabble.com.

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

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Using a Service from a static Domain method?

strayph
Thanks!  That works for me.

Strayph
Reply | Threaded
Open this post in threaded view
|

Re: Using a Service from a static Domain method?

burtbeckwith
In reply to this post by felipecao
That works, but keep in mind that the static holders are deprecated in 1.4 and will be removed in a future release.

Burt

> You're right, injections are on instance level, they're not present on a
> static context.
> To achieve what you want, you should use ApplicationHolder to retrieve a
> bean from Spring context:
>
> PageInfoService piService =
> ApplicationHolder.application.mainContext.getBean('pageInfoService ')
>
> On Fri, Jun 10, 2011 at 5:08 PM, strayph <[hidden email]> wrote:
>
> > Hello, all,
> >
> > I have a Page Domain model, and it uses a PageInfoService to get
> > information
> > about a Page.
> > I now want to be able to create Pages based on information from the
> > Service.
> > Since I was accessing other Pages via static domain methods, like
> > Page.find(), I thought I would create a new Page.retrieveByInfoType().
> >
> > However, the PageInfoService is injected into the objects, not the class,
> > or
> > at least, I haven't figured out how to access it.
> >
> > Am I missing something?
> >
> > Strayph
> >
> > --
> > View this message in context:
> > http://grails.1312388.n4.nabble.com/Using-a-Service-from-a-static-Domain-method-tp3589300p3589300.html
> > Sent from the Grails - user mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >    http://xircles.codehaus.org/manage_email
> >
> >
> >
>

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Using a Service from a static Domain method?

ld@ldaley.com
The recommended approach is to not use static methods for where you depend on other beans. Instead, move this method to a service.

On 11/06/2011, at 1:49 PM, Burt Beckwith wrote:

> That works, but keep in mind that the static holders are deprecated in 1.4 and will be removed in a future release.
>
> Burt
>
>> You're right, injections are on instance level, they're not present on a
>> static context.
>> To achieve what you want, you should use ApplicationHolder to retrieve a
>> bean from Spring context:
>>
>> PageInfoService piService =
>> ApplicationHolder.application.mainContext.getBean('pageInfoService ')
>>
>> On Fri, Jun 10, 2011 at 5:08 PM, strayph <[hidden email]> wrote:
>>
>>> Hello, all,
>>>
>>> I have a Page Domain model, and it uses a PageInfoService to get
>>> information
>>> about a Page.
>>> I now want to be able to create Pages based on information from the
>>> Service.
>>> Since I was accessing other Pages via static domain methods, like
>>> Page.find(), I thought I would create a new Page.retrieveByInfoType().
>>>
>>> However, the PageInfoService is injected into the objects, not the class,
>>> or
>>> at least, I haven't figured out how to access it.
>>>
>>> Am I missing something?
>>>
>>> Strayph
>>>
>>> --
>>> View this message in context:
>>> http://grails.1312388.n4.nabble.com/Using-a-Service-from-a-static-Domain-method-tp3589300p3589300.html
>>> Sent from the Grails - user mailing list archive at Nabble.com.
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>   http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>


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

    http://xircles.codehaus.org/manage_email