开发者

Does Groovy respect @PostConstruct?

开发者 https://www.devze.com 2023-03-08 19:53 出处:网络
Consider a bit of Groovy code like: def trainingCorpus开发者_运维技巧 = new CorpusNexmlReader(ignoreMissingTags: true,

Consider a bit of Groovy code like:

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. Now, if I add an @PostConstruct, will Groovy call it after all those setters?


No, it won't. The @PostConstruct is used only for spring beans (instantiated by the spring context, and not by you)


From the javadocs on the @PostConstruct annotation:

"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"

Groovy is not a dependency injection framework. It will not do any processing of annotations directed toward DI frameworks. If you were building your bean as a Spring bean using Groovy's spring builder, then it should respect that annotation.

Alternately, you could read and process that annotation yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消