现场注射究竟是怎样避免的呢? [英] What exactly is Field Injection and how to avoid it?

查看:134
本文介绍了现场注射究竟是怎样避免的呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了关于 Spring MVC Portlets 的一些帖子,不建议使用该字段注入。因为我试图得到一个所以我问自己,如果我正在使用字段注入,我无法回答。据我所知,如果您将 Bean 注入到具有 @Autowired field注入 >像这样:



CartController.java:

  ... 
@Autowired
私人购物车;
...

BookshopConfiguartion.java:

  @Configuration 
public class BookShopConfiguration {

@Bean
public Cart cart() {
return new Cart();
}
//更多配置

我的购物车.java 用于存储和提供有关购物车中图书的信息。



在我的研究过程中,我阅读了关于:



MyComponent.java:

  ... 
public class MyComponent {
private Cart cart;

@Autowired
public MyComponent(购物车购物车){
this.cart = cart;
}
...

两者的优点和缺点是什么这些类型的注射?

解决方案

注射类型



有三个选项可以将依赖关系注入到一个bean中:
1.通过构造函数
2.通过setter或其他方法
3.通过直接反映到字段

您正在使用选项3 - 即直接在您的字段上使用@Autowired。






注入指南



正如Spring所推荐的一般准则如下:




  • 对于强制依赖关系或针对可变性,请使用构造方法注入

  • 对于可选或可更改依赖关系,请使用setter注入

  • 在大多数情况下避免实地注射。






<强>野外注射缺点



现场注入的原因如下:




  • 您不能像构造函数注入一样创建不可变对象

  • 您的课程与DI容器紧密耦合,不能在其外部使用

  • 您的课程不能实例化(例如在单元测试中),没有反思,您需要DI容器来实例化它,这更像是集成测试

  • 您的真正依赖关系从外部隐藏而不被反映在你的界面 - 构造函数或方法

  • 真的很容易拥有10个依赖关系。如果你正在使用构造函数注入,那么具有10个参数的构造函数将会表明某些东西是腥的。但是您可以无限期地使用字段注入来添加注入的字段。拥有太多的依赖关系是一个红旗,这个类通常做的不止一件事,可能是违反单一责任原则。



< hr>

结论



根据您的需要,您应该主要构建注入或混合的建设者和安置者注射。现场注射有许多缺点,应该避免,唯一的优点是编写更方便,不超过所有的缺点。






进一步阅读



我写了一篇博客文章,为什么现场注入通常不会推荐 - 你可以在这里查看 - a href =http://vojtechruzicka.com/field-dependency-injection-considered-harmful/ =noreferrer>场依赖注入被认为有害。


I read in some posts about Spring MVC and Portlets that field injection is not recommended. Cause i'm trying to get a So i asked myself if i'm using field injection and i can't answer it. As i understand it field injection is if you inject a Bean into a attribute with @Autowired like this:

CartController.java:

...
@Autowired
private Cart cart;
...

BookshopConfiguartion.java:

@Configuration
public class BookShopConfiguration {

@Bean
public Cart cart(){
    return new Cart();
}
//more configuration

My Cart.java is used to store and provide information about the books in the cart.

During my research i read about constructor injection:

MyComponent.java:

...
public class MyComponent{
private Cart cart;

@Autowired
public MyComponent(Cart cart){
   this.cart = cart;
}
...

What are the advantages and the disadvantages of both of these types of injections?

解决方案

Injection types

There are three option how dependencies can be injected into a bean: 1. Through constructor 2. Through setters or other methods 3. Through reflection directly into fields

You are using option 3 - that is when you use @Autowired directly on your field.


Injection guidelines

General guideline, as reccomended also by Spring is following:

  • For mandatory dependencies or when aiming for mutability, use Constructor injection
  • For optional or changeable dependencies use setter injection
  • Avoid field injection in most of the cases.

Field injection Drawbacks

The reasons why field injection is frowned upon are following:

  • You cannot create immutable objects as with constructor injection
  • Your classes have tight coupling with DI container and cannot be used outside of it
  • Your classes cannot be instantiated (for example in unit tests) wthout reflection, you need DI container to instantiate them, which is more like integration tests
  • Your real dependencies are hidden from the outside and are not reflected in your interface - constructors or methods
  • It is really easy to have like 10 dependencies. If you were using constructor injection, having constructor with 10 arguments will signal that something is fishy. But you can add injected fields using field injection indefinitely. Having too many dependencies is a red flag that the class usually does more than one thing and it may be a violation of a Single Responsibility Principle.

Conclusion

Depending on your needs, you should you primarily constructor injection or some mix of constructor and setter injection. Field injection has many drawbacks and should be avoided and the only advantage is that it is more convenient to write, which does not outweigh all the cons.


Further Reading

I wrote a blog article why field injection is usually not reccomended - you can check it here - Field Dependency Injection Considered Harmful.

这篇关于现场注射究竟是怎样避免的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆