简体   繁体   中英

Groovy Spring DSL: constructing beans with properties of other beans

I have a bunch of Spring beans some of which need to be initialized from other beans, and some of which need to be initialized from properties of those other beans. Eg:

Foo {
}

Bar {
    String getBaz()
}

Qux {
    Qux(Foo foo, String baz)
}

I thought I could write something like

beans = {
    foo(Foo) {}
    bar(Bar) {}
    qux(Qux, ref('foo'), ref('bar').baz) {}
}

but obviously this doesn't work because ref('bar') isn't Bar, it's a RuntimeBeanReference .

In plain Spring (3+) what I want is apparently possible with spring expressions but I can't figure out the necessary syntax with the Grails Spring DSL. Can it be done?

I think you meant the classes to look like this:

class Foo {
}

class Bar {
   String baz
}

class Qux {
   Foo foo
   String baz

   Qux(Foo f, String b) {
      foo = f
      baz = b
   }
}

and the 2nd ref('foo') should have been ref('bar') . Then this will work:

beans = {
   foo(Foo)
   bar(Bar) {
      baz = 'wazzup'
   }
   qux(Qux, ref('foo'), '#{bar.baz}')
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM