|
Hi all,
it is possible to use the spring autowiring of services (and other) that works in controller and services also in non grails components, for example a pogo under src/groovy? How could this work?
Thx, mirko
|
|
Mirko Weber wrote:
> Hi all, > > it is possible to use the spring autowiring of services (and other) that > works in controller and services also in non grails components, for > example a pogo under src/groovy? How could this work? To use autowiring you have to have your objects managed by Spring, i.e. you can't just create instances of your objects using a constructor, you have to declare them as beans in resources.groovy or resources.xml and then either inject them into other beans or fetch them by name from the grailsApplication.mainContext. Given this, it's just a matter of adding the right configuration to the bean definition in resources.groovy: myBean(com.foo.MyBean) { bean -> bean.autowire = "byName" } or resources.xml: <bean id="myBean" class="com.foo.MyBean" autowire="byName" /> 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 |
|
Instead of using resources.xml / resources.groovy you could use the Spring annotations, Grails 1.2 supports this out the box, if using a prior version of grails just add '<component:resource-scan base-package="com.mypackage" />' to your resources.xml.
After that any class annotated with @Component is managed by Spring, you can use the @Resource(name="myService) annotation for dependency injection. @Component class MyNewService { //Auto wire by name example @Resource(name="myGrailsService") def myGrailsService //Auto wire by type example @Autowired AnotherGrailsSevice anotherGrailsService } I prefer annotations over XML, the choice is up to you.
|
|
Thanks for the responses. This is a good starting point for me (not much knowing about spring). I such a way I can get one instance of my pogo and autowire it.
But it is possible to get more instances from that pogo at runtime (like a domain-class) that are managed by spring and use the autowire of the services?
Thanks, mirko 2010/1/18 AdamE <[hidden email]>
|
|
Mirko Weber wrote:
> Thanks for the responses. This is a good starting point for me (not much > knowing about spring). I such a way I can get one instance of my pogo > and autowire it. > > But it is possible to get more instances from that pogo at runtime (like > a domain-class) that are managed by spring and use the autowire of the > services? Yes, set your bean to "prototype" scope rather than the default "singleton". Then each time you ask for it from the application context you will get a new instance. In resources.xml add scope="prototype" to the <bean> element, or if you're using @Component annotations you need to add a @Scope("prototype") annotation to the class as well (the @Scope annotation is in org.springframework.context.annotation). 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 |
|
Thanks, that are the right hints.
One little thing: I get it working with the resource.groovy style (like beans = { myBean(MyBean.class){bean ->
bean.autowire = true bean.scope = "prototype" }
} ) but when I use the annotation I don't get a bean. My pogo is located under src/groovy in a package and I use such annotation: @Component("myBean")
@Scope("prototype") class MyBean {..} When I boot in grails shell an run ctx.getBean("myBean") it works with the resource.groovy style but not with the annotation style.
But this is a minor thing....at last it works, thanks to Ian+Adam. Thx, Mirko 2010/1/19 Ian Roberts <[hidden email]>
|
|
Ok, I also get the annotaions working, describe it http://mirkoweber.blogspot.com/2010/01/using-spring-annotation-in-pogo-inside.html
Thanks for the help, Mirko
2010/1/19 Mirko Weber <[hidden email]> Thanks, that are the right hints. |
|
Its not documented anywhere but here as far as I know. It would be a good idea to update the release notes as this stung us too and has us scratching our head
but you need to set grails.spring.bean.packages = ['com.foo.stuff','com.foo.otherstuff'] Then @Component will work on your myBean(you don't need to give it a name like your example below if your ok with the bean name being the same) Then in your controller you can just do def myBean and it should get injected On Jan 19, 2010, at 5:15 PM, Mirko Weber wrote: Ok, I also get the annotaions working, describe it http://mirkoweber.blogspot.com/2010/01/using-spring-annotation-in-pogo-inside.html |
| Powered by Nabble | Edit this page |
