How can I access injected Grails beans in an abstract class method?

Steve Hole

I have an abstract class that implements much of the functionality inherited by a large number of concrete classes that are registered as beans. The beans are defined with autowiring on. For example:

abstract class MyAbstract {

    MyService myService
    MyBean myBean

    def doSomething() {
        def value = myService.something(myBean)
    }
}


class MyConcrete extends MyAbstract {
    def concreteField

    def doSomethingElse() {
         def value = myService.somethingElse(myBean)
    }
}

conf/spring/resources.groovy:

myConcrete(MyConcrete) { bean ->
    bean.autowire = true
    myBean = ref(MySpecificBeanImpl)
}

My Problem:

When I run the method doSomethingElse in a MyConcrete instance, everything works as expected and the myService and myBean values are filled in with the proper values by DI. When I execute the doSomething method in a MyConcrete instance, both the myService and myBean values are null. It appears that the DI values are not visible in the abstract method inherited by the subclass. That really sucks.

I can manually access the values using a context holder in the method or I can pass the values from the subclass to the abstract parent class using a modified method signature that accepts those values as parameters, but these are no good solutions. It completely breaks the usefulness of abstract class implementations and requires a lot of replicated code that I don't want to have to maintain.

Even worse, in my specific case, the value of myBean is actually different for each concrete class, explicitly wired in the resources.groovy file, so the generic holder approach doesn't work.

I've looked through a number of posts relating to this including Grails services in abstract class without much result in figuring out what is going on. The abstract bean definition seems to be about abstracting the bean definition properties and doesn't have anything to do with abstract classes and subclass inheritance.

(1) Is this a limitation in the Grails/Spring DI support? (2) Is there something else I need to do wrt the abstract class?

Jeff Scott Brown

You have left out some details and I have had to make some assumptions but I have created an app with something similar to what you are describing. The project at https://github.com/jeffbrown/abstractbeanmethods contains the following and appears to work:

src/groovy/demo/MyAbstract.groovy package demo

abstract class MyAbstract {

    MyService myService
    MyBean myBean

    def doSomething() {
        myService.something(myBean)
    }
}

src/groovy/demo/MyConcrete.groovy

package demo

class MyConcrete extends MyAbstract {
    def doSomethingElse() {
         def value = myService.somethingElse(myBean)
    }
}

grails-app/conf/spring/resources.groovy

// Place your Spring DSL code here
beans = {
    myBeanImpl demo.MySpecificBeanImpl

    myConcrete(demo.MyConcrete) { bean ->
        bean.autowire = true
        myBean = ref('myBeanImpl')
    }
}

src/groovy/demo/MySpecificBeanImpl.groovy package demo

class MySpecificBeanImpl implements MyBean {
}

src/groovy/demo/MyBean.groovy

package demo

interface MyBean {}

grails-app/service/demo/MyService.groovy package demo

class MyService {

    def something(MyBean bean) {
        "Bean class name is ${bean.class.name} in MyService.something() method"
    }
    def somethingElse(MyBean bean) {
        "Bean class name is ${bean.class.name} in MyService.somethingElse() method"
    }
}

grails-app/controllers/demo/DemoController.groovy package demo

class DemoController {
    def myConcrete
    def index() {
        def sb = new StringBuffer()
        sb << myConcrete.doSomething()
        sb << " and "
        sb << myConcrete.doSomethingElse()
        render sb
    }
}

You may find some significant difference between something there and someting that you are doing. If you can isolate a problematic scenario in that sample app, or provide a runnable version of your code then I will be happy to straighten it out for you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

can I use invoke to abstract class method

From Dev

How i can design these with abstract class

From Dev

C++: How can I access a method of an inner class?

From Dev

Android:How can I access a value from a method of a class?

From Dev

How can I access a method defined with new keyword in a derived class

From Dev

How can I access the value of a private attribute in an abstract class in a subclass object?

From Dev

How can I access the value of a private attribute in an abstract class in a subclass object?

From Dev

how can i implement specific method from two different abstract class?

From Dev

How to Access method from a sub- abstract class

From Dev

How to Access method from a sub- abstract class

From Dev

Can I make a static factory method in an abstract class?

From Dev

Why can I call an abstract class method in Python?

From Dev

Grails beans in resources.groovy not injected into service

From Dev

I can't access a method in a class

From Dev

Why can I abstract override an abstract method?

From Dev

How can we identify if a method belongs to an abstract class or an interface?

From Dev

How can we use the abstract getScreenSize() method in Toolkit class?

From Dev

How can I use the simple class name in Spring beans?

From Dev

How can I use the simple class name in Spring beans?

From Dev

How to call a method in an abstract class

From Dev

How can I access a class data member from a method within the same class?

From Dev

how can i test an abstract method in python 2.6

From Dev

how can i test an abstract method in python 2.6

From Dev

How can I test an abstract class using Espresso?

From Dev

Java abstract class - how can I best solve this?

From Dev

How can I retrieve the derived class from a list of abstract classes?

From Dev

How can I get name of interface from abstract class in PHP

From Dev

How can I instantiate an abstract class to remove a NullPointerException?

From Dev

How can I retrieve the derived class from a list of abstract classes?

Related Related

  1. 1

    can I use invoke to abstract class method

  2. 2

    How i can design these with abstract class

  3. 3

    C++: How can I access a method of an inner class?

  4. 4

    Android:How can I access a value from a method of a class?

  5. 5

    How can I access a method defined with new keyword in a derived class

  6. 6

    How can I access the value of a private attribute in an abstract class in a subclass object?

  7. 7

    How can I access the value of a private attribute in an abstract class in a subclass object?

  8. 8

    how can i implement specific method from two different abstract class?

  9. 9

    How to Access method from a sub- abstract class

  10. 10

    How to Access method from a sub- abstract class

  11. 11

    Can I make a static factory method in an abstract class?

  12. 12

    Why can I call an abstract class method in Python?

  13. 13

    Grails beans in resources.groovy not injected into service

  14. 14

    I can't access a method in a class

  15. 15

    Why can I abstract override an abstract method?

  16. 16

    How can we identify if a method belongs to an abstract class or an interface?

  17. 17

    How can we use the abstract getScreenSize() method in Toolkit class?

  18. 18

    How can I use the simple class name in Spring beans?

  19. 19

    How can I use the simple class name in Spring beans?

  20. 20

    How to call a method in an abstract class

  21. 21

    How can I access a class data member from a method within the same class?

  22. 22

    how can i test an abstract method in python 2.6

  23. 23

    how can i test an abstract method in python 2.6

  24. 24

    How can I test an abstract class using Espresso?

  25. 25

    Java abstract class - how can I best solve this?

  26. 26

    How can I retrieve the derived class from a list of abstract classes?

  27. 27

    How can I get name of interface from abstract class in PHP

  28. 28

    How can I instantiate an abstract class to remove a NullPointerException?

  29. 29

    How can I retrieve the derived class from a list of abstract classes?

HotTag

Archive