简体   繁体   English

Groovy 是否尊重@PostConstruct?

[英]Does Groovy respect @PostConstruct?

Consider a bit of Groovy code like:考虑一些 Groovy 代码,例如:

def trainingCorpus = new CorpusNexmlReader(ignoreMissingTags: true, 
                                           model: model, source: corpus,
                               abideByUse: true, useListSource: true,
                               listSourceDir: corpus + "/all/",
                               listSource: corpus + "/all/split_all.txt",
                               tagMap: tagMap
                               )

This helpfully uses the no-args constructor to make the object, and then makes calls to all the setters corresponding to the named parameters.这有助于使用无参数构造函数来生成 object,然后调用与命名参数对应的所有设置器。 Now, if I add an @PostConstruct, will Groovy call it after all those setters?现在,如果我添加一个@PostConstruct,Groovy 会在所有这些设置器之后调用它吗?

No, it won't.不,不会的。 The @PostConstruct is used only for spring beans (instantiated by the spring context, and not by you) @PostConstruct仅用于 spring bean(由 spring 上下文实例化,而不是由您实例化)

From the javadocs on the @PostConstruct annotation:从 @PostConstruct 注释上的 javadocs :

"The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service. This annotation MUST be supported on all classes that support dependency injection" “PostConstruct 注释用于需要在完成依赖注入以执行任何初始化后执行的方法。必须在 class 投入使用之前调用此方法。必须在所有支持依赖注入的类上支持此注释”

Groovy is not a dependency injection framework. Groovy 不是依赖注入框架。 It will not do any processing of annotations directed toward DI frameworks.它不会对指向 DI 框架的注释进行任何处理。 If you were building your bean as a Spring bean using Groovy's spring builder, then it should respect that annotation.如果您使用 Groovy 的 spring 构建器将 bean 构建为 Spring bean,那么它应该尊重该注释。

Alternately, you could read and process that annotation yourself.或者,您可以自己阅读和处理该注释。

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

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