简体   繁体   中英

Does Groovy respect @PostConstruct?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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