简体   繁体   English

何时何时不使用IOC /依赖注入?

[英]When to and When not to use IOC/Dependency Injection?

I am working with several Spring MVC web applications and I use the getter/setter dependency injection and configure all my beans in my app-servlet.xml file. 我正在使用几个Spring MVC Web应用程序,我使用getter / setter依赖注入并在app-servlet.xml文件中配置我的所有bean。

I believe that I am following convention with most of the properties and beans that I am injecting into my controller beans such as my DAO's and other beans that I have specified in either my applicationContext.xml or in the app-servlet.xml 我相信我遵循惯例,我将大部分属性和bean注入我的控制器bean,例如我的DAO和我在applicationContext.xmlapp-servlet.xml中指定的其他bean

As my applications have been getting more complex and larger, the beans in these files have been filling up with more properties that are being injected. 随着我的应用程序变得越来越复杂和越来越大,这些文件中的bean已经填满了更多正在注入的属性。

My question is, where is the line, or what is the convention on what should be injected, and what should be specified as an instance field/variable in the controller. 我的问题是,行在哪里,或者应该注入什么的约定,以及应该在控制器中指定为实例字段/变量的内容。

Sometimes I am faced with a situation if I am going to specify the value for a field in the actual controller, or if I am going to inject the value into that controllers bean. 如果我要在实际控制器中指定字段的值,或者如果我要将值注入到控制器bean中,有时我会面临一种情况。

For example, I am using velocity template library for sending my emails. 例如,我正在使用速度模板库来发送我的电子邮件。 There is a path to the directory of my Velocity Templates. 我的Velocity模板的目录有一条路径。 I am faced with doing one of the following. 我面临着以下其中一项。

In my Controller I could specify the value 在我的控制器中,我可以指定值

 private String basePath = "/path/to/velocity/templates";

Or in my Controller bean I could inject the same value into that controller 或者在我的Controller bean中,我可以将相同的值注入该控制器

<property name="basePath" value="/path/to/velocity/templates"/>

and in my class I would have the getters/setters for this injected value. 在我的班上,我会得到这个注入值的getter / setter。

I am not sure where to draw the line for each. 我不知道在哪里划线。

I normally inject based on: 我通常注入基于:

  1. whether I need to change this value per environment/deployment (eg using a PropertyPlaceholderConfiguration ). 是否需要根据环境/部署更改此值(例如,使用PropertyPlaceholderConfiguration )。
  2. whether I need to mock something out for testing. 我是否需要嘲笑某些东西进行测试。
  3. whether someone else is likely to consume this class in the future and want to do one of the above. 是否其他人可能在将来使用此课程,并希望做上述其中一项。

If you're not careful, then your configurations can become a mass of injected properties (I prefer to use constructor arguments so I can guarantee correct instantiation, but that's another story). 如果你不小心,那么你的配置可以成为大量的注入属性(我更喜欢使用构造函数参数,所以我可以保证正确的实例化,但这是另一个故事)。 The above guidelines work well in limiting the number of injections whilst permitting flexibility. 上述指南在限制注射次数的同时可以灵活地进行。

Do NOT plan for "flexibility" up front. 不要预先计划“灵活性”。 Usually you ain't gonna need it. 通常你不需要它。

When the time comes and you really need it, then refactor your code. 什么时候到来,你真的需要它,然后重构你的代码。

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

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