简体   繁体   中英

Does Groovy have support for something like Ruby Modules?

Ruby modules make things like passing a database connection or other dependencies to various objects much easier while allowing for separation of concerns. Does Groovy support a similar functionality? And if so what is it called?

In ruby modules are used either as mixins or to namespace a class (eg Net::HTTP ).

To mixin the behavior you can use @mixin annotation. like examples here http://groovy.codehaus.org/Category+and+Mixin+transformations .

To namespace, groovy uses same mechanism as java ie using packages (eg groovy.sql.Sql ).

I am not sure if that answered your question or not. But for dependency injection, while its common to do it mixin way in ruby (or even in scala/play), I have not seen it done a lot using @mixin in groovy. Usually a DI container like spring is used.

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