The AnnotationConfigApplicationContext class has the following:
public AnnotationConfigApplicationContext(Class<?>... componentClasses)
Create a new AnnotationConfigApplicationContext, deriving bean definitions from
the given component classes and automatically refreshing the context.
Parameters:
componentClasses - one or more component classes — for example, @Configuration classes
and
public void register(Class<?>... componentClasses)
Register one or more component classes to be processed.
Note that AbstractApplicationContext.refresh() must be called in order
for the context to fully process the new classes.
Specified by:
register in interface AnnotationConfigRegistry
Parameters:
componentClasses - one or more component classes — for example, @Configuration classes
See Also:
scan(String...)
AbstractApplicationContext.refresh()
Observe both, the constructor and the register method work with Class<?>... componentClasses and the latter requires to call the refresh method. Through the source code that version of the constructor does the register and refresh methods call
Therefore, when is mandatory use directly the register and refresh methods over the constructor version?
It was reported at SO at:
No answer so far, would be nice if can be expanded at both Spring Reference documentation and javadoc
Comment From: snicoll
The Javadoc already states the constructors that will trigger a refresh.
Therefore, when is mandatory use directly the register and refresh methods over the constructor version?
Such strong term are not necessary. I think this is yet another duplicate of #30385. If you want to further configure the context, for instance by specifying individual classes, and then trigger component scanning, you need to use the methods simply because the constructor does not allow you to do those at once. This is relatively straightforward and I don't think we need anything here.