为什么 Google Guice 的依赖注入根本没有配置文件?
我正在查看 Google Guice 作为 DI 框架,但我有点困惑:为什么根本就没有配置文件吗?
我找到了关于这个问题的部分解释,但它仍然是不清楚如果没有配置文件,我将如何设置我的组件角色(或我需要使用开关的任何其他东西)。
任何帮助表示赞赏!
I am checking out Google Guice as DI framework but I am a bit puzzled: why there's no configuration file at all?
I found a partial explanation on this question but it is still not clear how I would be able to set my component roles (or any other thing I need to use a switch) without a config file.
Any help appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
配置位于代码中而不是配置文件中,这对于许多场景来说是一个有效的决定。
是的,这意味着如果您想发布一种不同的方式来管理您的应用程序,您必须重建(可能只是模块) - 当然,如果您愿意,您仍然可以从命令行参数、属性文件等中获取一些配置值到。
如果您经常需要更改应用程序管道并且除了单个文件之外不想重新部署任何内容,Guice 可能不适合您。 另一方面,如果您使用 DI 的主要原因是让代码更清晰,并且在生产中您总是会使用相同的管道(或足够接近),那么 Guice 是一个不错的选择 - 通常有一些您想要的逻辑无论如何,在整理管道和通常难以以声明方式描述/构造的组件时使用。
不同的 DI 框架有不同的优点和权衡 - 使用最适合您的应用程序的一种。
The configuration is in the code instead of config files, which is a valid decision for many scenarios.
Yes, it means that you have to rebuild (possibly just the modules) if you want to release a different way of plumbing your application - although of course you could still get some configuration values from command-line arguments, properties files etc if you want to.
If you regularly need to change your application plumbing and don't want to redeploy anything but a single file, Guice may not be for you. If on the other hand your main reason for using DI is to make your code clearer, and in production you'll always really use the same plumbing (or close enough) then Guice is a good option - there are often bits of logic you want to use when sorting out the plumbing anyway, and components which are generally hard to describe/construct declaratively.
Different DI frameworks have different benefits and trade-offs - use whichever one is most suitable for your application.
如果您愿意的话,使用配置文件引入 boostrapping 是很简单的。 我们将 Guice 与一个简单的 API 一起使用,该 API 加载真正需要参数化的属性文件。 这可以与 @Named 注释等一起使用,当然,您可以在模块中使用一些条件(尽管最好不要过度这样做)。
这是我们的引导部分如何设置的示例:
其中“设置”读取我们的属性文件等。 现在,开发/生产以及特定于部署的特定设置覆盖似乎可以为我们完成这项工作,但如果我们显然愿意的话,我们可以更进一步。
It is trivial to introduce boostrapping using configuration files if you are so inclined. We use Guice together with a simple API that loads property files where the things go that really need to be parameterized. This can be used together with @Named annotations and such, and of course you can have some conditionals in modules (though it is a good idea not to over-do that).
This is an example of how part of our bootstrapping is set up:
Where Settings is what reads our properties files and such. Right now development/ production together with specific settings overrides specific for deployments seems to do the job for us, but we could go further with this if we wanted to obviously.
大多数 DI 配置从一个部署到另一个部署都是相同的,因此可以很好地使用代码进行配置,这使得 Guice 的配置非常简洁,并且您可以获得编译时类型检查、重构工具、代码导航等的好处
。从部署到另一个部署的一些变化,例如数据库用户名和密码配置,您可以自己编写所需的代码。 编写读取配置文件(可能是属性文件)、解析参数并将它们绑定到 Guice 模块中的代码,以便您的应用程序可以访问它们。 执行此操作所需的代码不需要很多行代码。
Most of the DI configuration will be the same from one deployment to another, so they can very well be configured using code, which makes Guice's configuration very terse and you get the benefits of compile time type checking, refactoring tools, code navigation etc.
For those few things that change from deployment to another, such as the database username and password configuration, you can write the needed code yourself. Write code which reads the configuration file (maybe a properties file), parses the parameters, and binds them in your Guice modules so that your application gets access to them. The code that is needed for doing that won't take many lines of code.
Guice 中的许多配置都是通过 @Inject 注释隐式进行的。 项目的大量复杂性来自于大量的项目工件。 Java 文件、Xml 文件、属性文件、数据库、参数……Guice 尝试通过不使用配置文件来消除部分复杂性。
在编译时重新连接您的应用程序很容易。 您很可能只需要编辑您的模块类。 对于 Guice 处理的大多数类,您根本不需要配置,而只需要在正确的位置 @Inject - 当您有同一接口的两个不同实现时,或者当您想从以下位置注入类时,您只需要配置任何内容使用 Provider 类的外部库。
A lot of configuration in Guice is implicit, via the @Inject Annotation. A great deal of complexity in projects comes from a big number of project Artifacts. Java files, Xml files, Properties files, databases, parameters.. Guice tries to remove a part of this complexity by not using config files.
Re-wiring your application is easy at compile time. Most likely you will only need to edit your module class. For most classes handeled by Guice, you will need no config at all, but only @Inject in the right places - you will only need to configure anything when you have two different Implementations of the same Interface - or when you want to inject classes from external libraries using Provider classes.
不确定文件是什么意思,但 Guice 允许您通过 Binder 和自定义 提供商。
Not sure what you mean by file but Guice lets you change implementaions via Binder and custom Providers .
我最近创建了以下项目。
http://code.google.com/p/guice-property-injector/< /a>
它是 WIP,但允许运行时从基于环境的属性文件注入属性。
I have recently created the following project.
http://code.google.com/p/guice-property-injector/
It is WIP but allows runtime injection of properties from a property file based on environment.