简体   繁体   English

Groovy是否支持Ruby模块之类的东西?

[英]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. Ruby模块使诸如将数据库连接或其他依赖项传递给各种对象的事情变得更加容易,同时又使关注点分离。 Does Groovy support a similar functionality? Groovy是否支持类似的功能? And if so what is it called? 如果是这样,它叫什么?

In ruby modules are used either as mixins or to namespace a class (eg Net::HTTP ). 在ruby中,模块既可以用作mixins,也可以用作类的名称空间(例如Net::HTTP )。

To mixin the behavior you can use @mixin annotation. 要混合行为,可以使用@mixin批注。 like examples here http://groovy.codehaus.org/Category+and+Mixin+transformations . 例如此处的示例http://groovy.codehaus.org/Category+and+Mixin+transformations

To namespace, groovy uses same mechanism as java ie using packages (eg groovy.sql.Sql ). 对于命名空间,groovy使用与java相同的机制,即使用包(例如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. 但是对于依赖项注入,虽然通常在ruby中(甚至在scala / play中)进行混合,但我还没有看到它在groovy中使用@mixin做了很多工作。 Usually a DI container like spring is used. 通常使用DI容器,例如spring。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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