Quantcast

Spring bean name for Controller?

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Spring bean name for Controller?

redcoat
I'm trying to debug some autowiring issues...

What bean name is used for MyController?  I expected it to be lowercase first letter, so "myController".  But when Spring is autowiring by name, my "def myController" isn't autowired.

Can anyone shed some light on this?


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

Jeff Brown-3
David Hay wrote:
> I'm trying to debug some autowiring issues...
>
> What bean name is used for MyController?  I expected it to be lowercase
> first letter, so "myController".  But when Spring is autowiring by name,
> my "def myController" isn't autowired.
>
> Can anyone shed some light on this?
>
>

The bean name is the fully qualified name of the controller class, like
"com.demo.DemoController".

Why is it that you want to inject a controller into some other bean?



jb

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

Ian Roberts
In reply to this post by redcoat
On 21/05/2012 22:12, David Hay wrote:
> I'm trying to debug some autowiring issues...
>
> What bean name is used for MyController?  I expected it to be lowercase
> first letter, so "myController".  But when Spring is autowiring by name,
> my "def myController" isn't autowired.
>
> Can anyone shed some light on this?

I believe it's just the fully-qualified class name (including package,
so a bean name like com.example.MyController), which isn't suitable for
autowiring.  Controllers are prototype scoped so they're not normally
the kind of thing you'd want to autowire into other objects in the same
way you do with (singleton) services.

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
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

redcoat
thanks for the replies Jeff/Ian.

I'm not trying to inject a controller into another bean ;)  Rather, I'm trying to figure out why a Controller is not autowired by using "def myController" in a Spock test that extends IntegrationSpec.

You would expect that to work, right?  It uses GrailsTestAutowirer's autowire(bean) method (copied below).  Everything works fine in the same class when I inject MyService, using either "def myService" or "MyService myService".  I've tried "MyController myController" and it still doesn't get injected.

Hence my question - any idea why things would be different for a controller?  We do have a packaging scheme, so the controller and test are both in the same package.

Help much appreciated - I'm a little stumped!

David

/**
 * Convenience class to autowire test classes
 */
class GrailsTestAutowirer {

    ApplicationContext applicationContext

    GrailsTestAutowirer(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext
    }

    /**
     * Autowires the bean by name, processes any autowiring annotations,
     * and set's the applicationContext if it implements ApplicationContextAware.
     */
    void autowire(bean) {
        def beanFactory = applicationContext.autowireCapableBeanFactory
        beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false)
        beanFactory.initializeBean(bean, bean.class.name)

        def annotationProcessor = beanFactory.createBean(AutowiredAnnotationBeanPostProcessor, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false)
        annotationProcessor.processInjection(bean)

        if (bean instanceof ApplicationContextAware && applicationContext != null) {
            bean.setApplicationContext(applicationContext)
        }
    }
}






On Mon, May 21, 2012 at 5:45 PM, Ian Roberts <[hidden email]> wrote:
On 21/05/2012 22:12, David Hay wrote:
> I'm trying to debug some autowiring issues...
>
> What bean name is used for MyController?  I expected it to be lowercase
> first letter, so "myController".  But when Spring is autowiring by name,
> my "def myController" isn't autowired.
>
> Can anyone shed some light on this?

I believe it's just the fully-qualified class name (including package,
so a bean name like com.example.MyController), which isn't suitable for
autowiring.  Controllers are prototype scoped so they're not normally
the kind of thing you'd want to autowire into other objects in the same
way you do with (singleton) services.

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
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

bksaville
It may depend on the version of grails, but I know in Grails 2, the controller constructor is actually overridden (using metaClass magic) to return the wired controller.  So try doing def controller = new MyController() and see if that works.  It has for me, even in 1.3.7 I believe.

-Brian

On Mon, May 21, 2012 at 7:29 PM, David Hay <[hidden email]> wrote:
thanks for the replies Jeff/Ian.

I'm not trying to inject a controller into another bean ;)  Rather, I'm trying to figure out why a Controller is not autowired by using "def myController" in a Spock test that extends IntegrationSpec.

You would expect that to work, right?  It uses GrailsTestAutowirer's autowire(bean) method (copied below).  Everything works fine in the same class when I inject MyService, using either "def myService" or "MyService myService".  I've tried "MyController myController" and it still doesn't get injected.

Hence my question - any idea why things would be different for a controller?  We do have a packaging scheme, so the controller and test are both in the same package.

Help much appreciated - I'm a little stumped!

David

/**
 * Convenience class to autowire test classes
 */
class GrailsTestAutowirer {

    ApplicationContext applicationContext

    GrailsTestAutowirer(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext
    }

    /**
     * Autowires the bean by name, processes any autowiring annotations,
     * and set's the applicationContext if it implements ApplicationContextAware.
     */
    void autowire(bean) {
        def beanFactory = applicationContext.autowireCapableBeanFactory
        beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false)
        beanFactory.initializeBean(bean, bean.class.name)

        def annotationProcessor = beanFactory.createBean(AutowiredAnnotationBeanPostProcessor, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false)
        annotationProcessor.processInjection(bean)

        if (bean instanceof ApplicationContextAware && applicationContext != null) {
            bean.setApplicationContext(applicationContext)
        }
    }
}






On Mon, May 21, 2012 at 5:45 PM, Ian Roberts <[hidden email]> wrote:
On 21/05/2012 22:12, David Hay wrote:
> I'm trying to debug some autowiring issues...
>
> What bean name is used for MyController?  I expected it to be lowercase
> first letter, so "myController".  But when Spring is autowiring by name,
> my "def myController" isn't autowired.
>
> Can anyone shed some light on this?

I believe it's just the fully-qualified class name (including package,
so a bean name like com.example.MyController), which isn't suitable for
autowiring.  Controllers are prototype scoped so they're not normally
the kind of thing you'd want to autowire into other objects in the same
way you do with (singleton) services.

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
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

Jeff Brown-3
In reply to this post by redcoat
David Hay wrote:
> thanks for the replies Jeff/Ian.
>
> I'm not trying to inject a controller into another bean ;)  Rather, I'm
> trying to figure out why a Controller is not autowired by using "def
> myController" in a Spock test that extends IntegrationSpec.
>
> You would expect that to work, right?

No, I wouldn't expect that to work.  If there were a bean named
"myController" then I would expect it to work but I don't think there is
a bean with that name.  The bean name is more like
"com.something.MyController", isn't it?



jb


--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

redcoat
That's what I'm finally starting to realize.  It is confusing to me that the service is injected whereas the controller is not.  So, apparently the name of the bean is the fully qualified com.xxx.MyController whereas the name of this service bean is just  myService?

Any idea if this is documented anywhere?  

On Tuesday, May 22, 2012, Jeff Brown wrote:
David Hay wrote:
thanks for the replies Jeff/Ian.

I'm not trying to inject a controller into another bean ;)  Rather, I'm
trying to figure out why a Controller is not autowired by using "def
myController" in a Spock test that extends IntegrationSpec.

You would expect that to work, right?

No, I wouldn't expect that to work.  If there were a bean named "myController" then I would expect it to work but I don't think there is a bean with that name.  The bean name is more like "com.something.MyController", isn't it?



jb


--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

  http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

Jeff Brown-3
David Hay wrote:
> That's what I'm finally starting to realize.  It is confusing to me that
> the service is injected whereas the controller is not.  So, apparently
> the name of the bean is the fully qualified com.xxx.MyController whereas
> the name of this service bean is just  myService?
>
> Any idea if this is documented anywhere?
>

The bean name for services is documented in the user guide.  See
http://grails.org/doc/latest/guide/services.html#dependencyInjectionServices.

The bean name for controllers is probably not documented because it
isn't intended to generally be used for DI or for anything that an app
would interact with directly.



jb
--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

Jeff Brown-3
Jeff Brown wrote:

> David Hay wrote:
>> That's what I'm finally starting to realize. It is confusing to me that
>> the service is injected whereas the controller is not. So, apparently
>> the name of the bean is the fully qualified com.xxx.MyController whereas
>> the name of this service bean is just myService?
>>
>> Any idea if this is documented anywhere?
>>
>
> The bean name for services is documented in the user guide. See
> http://grails.org/doc/latest/guide/services.html#dependencyInjectionServices.
>
>
> The bean name for controllers is probably not documented because it
> isn't intended to generally be used for DI or for anything that an app
> would interact with directly.
>
>
>
> jb

I still don't understand why you are retrieving the bean to begin with.
  The the @TestFor(com.xxx.MyController) stuff not work as it should?



jb

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

redcoat
That is my problem, exactly!  @TestFor only works for UNIT tests - I'm trying to have MyController injected into an INTEGRATION test (extends IntegrationSpec)

Seeing as the Service is injected just fine, I'm struggling to find a clean way to do that for a Controller.  Seems to me, there should be an easy way to do that. 

cheers,

David


On Tue, May 22, 2012 at 10:50 AM, Jeff Brown <[hidden email]> wrote:
Jeff Brown wrote:
David Hay wrote:
That's what I'm finally starting to realize. It is confusing to me that
the service is injected whereas the controller is not. So, apparently
the name of the bean is the fully qualified com.xxx.MyController whereas
the name of this service bean is just myService?

Any idea if this is documented anywhere?


The bean name for services is documented in the user guide. See
http://grails.org/doc/latest/guide/services.html#dependencyInjectionServices.


The bean name for controllers is probably not documented because it
isn't intended to generally be used for DI or for anything that an app
would interact with directly.



jb

I still don't understand why you are retrieving the bean to begin with.  The the @TestFor(com.xxx.MyController) stuff not work as it should?




jb

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

  http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

bksaville
Did my message not make it through before?  Just instantiate the
controller – the constructor is overridden by grails in integration tests
to cleanly return the autowired controller.

def controller = new MyController()

-Brian

From: David Hay [mailto:[hidden email]]
Sent: Tuesday, May 22, 2012 9:01 AM
To: [hidden email]
Subject: Re: [grails-user] Re: Spring bean name for Controller?

That is my problem, exactly!  @TestFor only works for UNIT tests - I'm
trying to have MyController injected into an INTEGRATION test (extends
IntegrationSpec)

Seeing as the Service is injected just fine, I'm struggling to find a
clean way to do that for a Controller.  Seems to me, there should be an
easy way to do that.

cheers,

David

On Tue, May 22, 2012 at 10:50 AM, Jeff Brown <[hidden email]>
wrote:
Jeff Brown wrote:
David Hay wrote:
That's what I'm finally starting to realize. It is confusing to me that
the service is injected whereas the controller is not. So, apparently
the name of the bean is the fully qualified com.xxx.MyController whereas
the name of this service bean is just myService?

Any idea if this is documented anywhere?

The bean name for services is documented in the user guide. See
http://grails.org/doc/latest/guide/services.html#dependencyInjectionServic
es.


The bean name for controllers is probably not documented because it
isn't intended to generally be used for DI or for anything that an app
would interact with directly.



jb

I still don't understand why you are retrieving the bean to begin with.
 The the @TestFor(com.xxx.MyController) stuff not work as it should?




jb

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
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
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

Jeff Brown-3
In reply to this post by redcoat
David Hay wrote:

> That is my problem, exactly!  @TestFor only works for UNIT tests - I'm
> trying to have MyController injected into an INTEGRATION test (extends
> IntegrationSpec)
>
> Seeing as the Service is injected just fine, I'm struggling to find a
> clean way to do that for a Controller.  Seems to me, there should be an
> easy way to do that.
>
> cheers,
>
> David
>

Does ControllerIntegrationSpec handle that?



jb


--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

redcoat
In reply to this post by bksaville
Thanks Brian.  Agreed I can do that - just seems like there should be a way to have everything autowired without having to create it.  It would be nice to have something like @TestFor for integration tests too...

On Tue, May 22, 2012 at 11:05 AM, Brian Saville <[hidden email]> wrote:
Did my message not make it through before?  Just instantiate the
controller – the constructor is overridden by grails in integration tests
to cleanly return the autowired controller.

def controller = new MyController()

-Brian

From: David Hay [mailto:[hidden email]]
Sent: Tuesday, May 22, 2012 9:01 AM
To: [hidden email]
Subject: Re: [grails-user] Re: Spring bean name for Controller?

That is my problem, exactly!  @TestFor only works for UNIT tests - I'm
trying to have MyController injected into an INTEGRATION test (extends
IntegrationSpec)

Seeing as the Service is injected just fine, I'm struggling to find a
clean way to do that for a Controller.  Seems to me, there should be an
easy way to do that.

cheers,

David

On Tue, May 22, 2012 at 10:50 AM, Jeff Brown <[hidden email]>
wrote:
Jeff Brown wrote:
David Hay wrote:
That's what I'm finally starting to realize. It is confusing to me that
the service is injected whereas the controller is not. So, apparently
the name of the bean is the fully qualified com.xxx.MyController whereas
the name of this service bean is just myService?

Any idea if this is documented anywhere?

The bean name for services is documented in the user guide. See
http://grails.org/doc/latest/guide/services.html#dependencyInjectionServic
es.


The bean name for controllers is probably not documented because it
isn't intended to generally be used for DI or for anything that an app
would interact with directly.



jb

I still don't understand why you are retrieving the bean to begin with.
 The the @TestFor(com.xxx.MyController) stuff not work as it should?




jb

--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

---------------------------------------------------------------------
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
|  
Report Content as Inappropriate
star

Re: Spring bean name for Controller?

redcoat
In reply to this post by Jeff Brown-3
I'm kind of confused where that fits with Spock Plugin and Grails 1.3 / 2.  I believe that's superseded by IntegrationSpec for Grails 2+

On Tue, May 22, 2012 at 11:12 AM, Jeff Brown <[hidden email]> wrote:
David Hay wrote:
That is my problem, exactly!  @TestFor only works for UNIT tests - I'm
trying to have MyController injected into an INTEGRATION test (extends
IntegrationSpec)

Seeing as the Service is injected just fine, I'm struggling to find a
clean way to do that for a Controller.  Seems to me, there should be an
easy way to do that.

cheers,

David


Does ControllerIntegrationSpec handle that?




jb


--
Jeff Brown
SpringSource
http://www.springsource.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

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

  http://xircles.codehaus.org/manage_email



Loading...