java Spring:init-method、PostConstruct、afterPropertiesSet:什么时候使用一个而不是其他的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25528775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:11:08  来源:igfitidea点击:

Spring: init-method, PostConstruct, afterPropertiesSet : when to use one over others?

javaspring

提问by SR Nathan

There are lots of initialization options available in spring bean life cycle.

Spring bean 生命周期中有很多可用的初始化选项。

init-method, PostConstructannotation, afterPropertiesSet, Bean post-initializationand even class constructor. All these can be used for initializing a bean.

init-methodPostConstruct注解、afterPropertiesSet、 Bean post-initialization甚至类构造函数。所有这些都可以用于初始化一个 bean。

I got confused when to use one these over other. Moreover, is there any case we may need to use all these option in a single Bean? If yes please example would be good.

我很困惑何时使用这些而不是其他。此外,是否有任何情况我们可能需要在单个 Bean 中使用所有这些选项?如果是,请举例就好。

Really looking forward to get some great answers.

真的很期待得到一些很好的答案。

回答by alexander zak

The difference between using the constructor and the other options is that the constructor code is the first to be executed, while the other options will be called only after dependencies were injected into the bean (either from @Autowiredannotations or the XML file).

使用构造函数和其他选项的区别在于构造函数代码是最先执行的,而其他选项只有在依赖项注入 bean 之后才会被调用(来自@Autowired注释或 XML 文件)。

Code you write in the constructor will run while the bean's properties are still not initiated. All @Autowiredfields would be null. Sometimes this is what you want, but usually you want the code to run after properties are set.

您在构造函数中编写的代码将在 bean 的属性尚未启动时运行。所有@Autowired字段都将为null。有时这就是您想要的,但通常您希望代码在设置属性后运行。

Other than this, I do not see a difference, other then order of execution. I do not think there is a case you would want to have all options in the same class.

除此之外,我看不出有什么区别,除了执行顺序。我认为您不会希望在同一类中拥有所有选项。

回答by geoand

I would suggest that you only use the constructor where possible. There is one very very good reason to do so: testing

我建议您只在可能的情况下使用构造函数。这样做有一个非常好的理由:测试

When you are going to unit test a Spring bean, you'll want to be able to construct the class with minimum fuss. That means that you should only need to call the constructor, and not have to deal with calling various life-cycle methods on your own. The last thing you want when creating the class to be tested, is to have to know how the object is property initialized.

当您要对 Spring bean 进行单元测试时,您将希望能够以最小的麻烦来构造类。这意味着您应该只需要调用构造函数,而不必自己处理调用各种生命周期方法。创建要测试的类时,您最不想要的就是必须知道对象的属性是如何初始化的。

With Spring's constructor injection support you can easily inject other other beans or project properties into the constructor therefor being able to cover almost every scenario.

借助 Spring 的构造函数注入支持,您可以轻松地将其他其他 bean 或项目属性注入构造函数,从而能够涵盖几乎所有场景。