2011-04-13 145 views
1

当我使用Grails提供的Spring DSL时,可以执行构造函数注入。如果是这样,一个例子将不胜感激。使用Grails Spring DSL的依赖注入

如果构造函数注入是不可能的,有没有其他的方式可以注入spring bean而不需要依赖关系公共属性。在Java项目中使用Spring我能做到这一点

class Foo { 

    @Autowired 
    private Bar bar 
} 

它将自动装配的Bar依赖通过名称或使用@Autowired注解像你通常做键入

回答

4

,可以使用构造器注入即使使用BeanBuilder DSL

bb.beans { 
    exampleBean(MyExampleBean, "firstArgument", 2) { 
     someProperty = [1,2,3] 
    } 
} 

,每当你想引用其他豆类构造函数的参数,使用ref()方法

bb.beans { 
    exampleBean(MyExampleBean, "firstArgument", ref('anotherBean')) { 
     someProperty = [1,2,3] 
    } 
} 
+0

看到http://grails.org/doc/latest/guide/14.%20Grails%20and%20Spring.html#14.4%20The%20BeanBuilder%20DSL%20Explained – 2011-04-14 07:12:39

+0

这工作很好,但为什么不'constructorArgs = ref(' someBean')'也有效? – raffian 2012-09-14 17:39:55