Grails
  1. Grails
  2. GRAILS-7095

Dependency injection not working in webflows

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.3.5
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      Mac OS X 10.6.5, Grails 1.3.4, webflow plugin 1.3.6

      Description

      Creating this JIRA report as per suggestion in GRAILS-4141

      When creating a webflow in a controller, and then trying to access an injected object, a NullPointerException is thrown, for example:

      class MyController {
         def springSecurityService
        
         def myWebFlow = {
           
            def user = springSecurityService.principal 
      
             // "Cannot access principal on null object" thrown
         }
      

      Note that trying to manually retrieve the beans results in the same exception:

      class MyController implements ApplicationContextAware {
         ApplicationContext applicationContext
        
         def myWebFlow = {
           
            def springSecurityService = applicationContext.getBean("springSecurityService")
      
             // "Cannot access getBean on null object" thrown
         }
      

        Issue Links

          Activity

          Hide
          Ciaran Waters added a comment -

          I was wondering was their any update on this issue. I cannot use the webflow plugin in its current state.

          Show
          Ciaran Waters added a comment - I was wondering was their any update on this issue. I cannot use the webflow plugin in its current state.
          Hide
          John Sherlock added a comment -

          Also having issues with this so adding my vote to raise the priority... Is this only an issue with the latest version of the plugin? It seems like a fairly important requirement to have working. Is there a work around of any sort?

          Show
          John Sherlock added a comment - Also having issues with this so adding my vote to raise the priority... Is this only an issue with the latest version of the plugin? It seems like a fairly important requirement to have working. Is there a work around of any sort?
          Hide
          Vincent Russell added a comment -

          A workaround is to make your controller an InitializingBean with "implements InitializingBean {"

          And then you can use a method like this:
          def flowService
          def service
          def flowController

          void afterPropertiesSet()

          { flowService = service }

          in your flow you should be able to access flowService, which will be what should be injected for "service"

          Show
          Vincent Russell added a comment - A workaround is to make your controller an InitializingBean with "implements InitializingBean {" And then you can use a method like this: def flowService def service def flowController void afterPropertiesSet() { flowService = service } in your flow you should be able to access flowService, which will be what should be injected for "service"
          Hide
          mark takacs added a comment -

          This is what worked for us using grails/webflow 1.3.7

          I marked springSecurityService as transient and all my custom services as flow scoped.

          class SubscribeController {
              // NOTE: Needs to be transient to exist in webflow.   -tak May 2011
              def transient springSecurityService
          
              // injection of account info API  (service is static scope="flow")
              def accountService
          
              // injection of subscription API  (service is static scope="flow")
              def subscribeService
          
              /**
               *  Join/Subscribe Flow - usher the user through some data collection and finally trigger a user creation
               *  and subscription purchase
               *
               */
              def joinFlow = {
              ...
                 firstPage { ...  }
                 on("submit") {
                    ...
                    def isLoggedIn = springSecurityService.isLoggedIn()
                    ...
                 }
              ...
              }
          
          

          this seems to avoid springSecurityService being NPE'd inside the flow.

          Show
          mark takacs added a comment - This is what worked for us using grails/webflow 1.3.7 I marked springSecurityService as transient and all my custom services as flow scoped. class SubscribeController { // NOTE: Needs to be transient to exist in webflow. -tak May 2011 def transient springSecurityService // injection of account info API (service is static scope= "flow" ) def accountService // injection of subscription API (service is static scope= "flow" ) def subscribeService /** * Join/Subscribe Flow - usher the user through some data collection and finally trigger a user creation * and subscription purchase * */ def joinFlow = { ... firstPage { ... } on( "submit" ) { ... def isLoggedIn = springSecurityService.isLoggedIn() ... } ... } this seems to avoid springSecurityService being NPE'd inside the flow.
          Hide
          Donal added a comment -

          I'm using this workaround

          class CheckoutController {
          
              def checkoutFlow = {
          
                  action {
                      ShoppingService shoppingService = grailsApplication.mainContext.getBean('shoppingService')
                  }
              }
          }
          
          Show
          Donal added a comment - I'm using this workaround class CheckoutController { def checkoutFlow = { action { ShoppingService shoppingService = grailsApplication.mainContext.getBean('shoppingService') } } }

            People

            • Assignee:
              Unassigned
              Reporter:
              Dan Stieglitz
            • Votes:
              12 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

              • Created:
                Updated:
                Last Reviewed:

                Development