简体   繁体   English

当明确需要默认构造函数以及参数化构造函数时

[英]When default constructor is required explicitly along with parameterized constructor

I am trying to learn spring dependency injection. 我正在尝试学习spring依赖注入。 I want to know when there is need of default constructor in dependency injection and what happen if we do not use default constructor explicitly 我想知道依赖注入中何时需要默认构造函数,以及如果我们不明确使用默认构造函数会发生什么?

如果计划在不带任何参数的情况下实例化Bean,则必须提供一个默认的构造函数。

You should only provide a default constructor if there is a safe way to instantiate the bean that way - let the code document itself. 仅当存在安全地实例化bean的方法时,才应提供默认构造函数-让代码文档本身。

It is much better to include your bean's dependencies as constructor parameters, which allows you to make it very clear what your bean needs. 最好将bean的依赖项包含为构造函数参数,这样可以使您非常清楚bean的需求。 The following is very clear: 以下内容非常清楚:

@Autowired
public MyBean(WidgetService widgetService, NutService nutService, BoltService boltService) {

Whereas a default constructor for MyBean with the @Autowired annotation on various setters can get you into situations where the bean doesn't have all the dependencies it needs: 而MyBean的默认构造函数(在各种设置器上带有@Autowired注释)可以使您陷入Bean没有所需的所有依赖关系的情况:

public MyBean() {

This is very useful when instantiating the bean manually, for example when testing the object. 当手动实例化bean时,例如在测试对象时,这非常有用。

Also, don't confuse the needs of Hibernate and other mapping solutions which tend to need a default constructor as well as a parameterised one, with other objects such as Spring Beans which don't. 另外,不要混淆Hibernate和其他映射解决方案的需求,这些解决方案往往需要默认构造函数和参数化构造函数,而不需要其他对象,例如Spring Beans。 Mapping solutions tend to create an empty instance of the POJO (Plain Old Java Object) and then call setters to store values when unmarshalling from the database/XML/JSON/etc. 映射解决方案倾向于创建POJO(普通的旧Java对象)的空实例,然后在从数据库/ XML / JSON / etc中解组时调用设置器来存储值。

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

相关问题 什么时候必须在Java中使用默认构造函数和参数化构造函数? - When is mandatory to have a default constructor along with parameterized constructor in Java? 如何从默认构造函数调用参数化构造函数? - how to call parameterized constructor from default constructor? 调用类的参数化/默认构造函数? - Invoking parameterized/default constructor of a class? 当没有其他构造函数时,是否有理由显式编写默认构造函数? - Is there a reason to explicitly code a default constructor when there are no other constructors? 从参数化构造函数调用默认构造函数的任何方法? - Any way to call the default constructor from a parameterized constructor? JPA:是否需要为空的默认构造函数? - JPA: is the default constructor required to be empty? 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class? 何时在Java中使用参数化方法与参数化构造函数 - When to use Parameterized Method vs Parameterized Constructor in Java 在显式定义构造函数时,没有在类中定义0参数的构造函数 - No constructor with 0 arguments defined in class when explicitly define constructor hibernate 实体上的参数化构造函数 - Parameterized constructor on hibernate entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM