Here is some simple code that will show the pattern:
ClassPathXmlApplicationContextctx = ...
ctx.refresh(); // This works as expectedctx.close(); // This closes the context correctlyctx.refresh(); // This restarts the context properlyctx.close(); // This does NOT close the context
I traced it down to the "closed" field in AbstractApplicationContext, it is initialized to false, it is set to true when closing, but it is never set again to false after it is restarted. This causes the doClose() method to do nothing.
Affects: 4.1.7, 4.2 GA
#18132 Close processing triggered by shutdown hook isn't thread safe
close() isn't actually defined for repeated calls on the same ApplicationContext instance, in contrast to refresh() which - on a re-refreshable context - does allow for repeated calls. In a sequence as yours, you'd just call refresh() twice, with a single close() call at the end.
That said, it should be easy enough to allow for repeated closing and refreshing here. There might be subtleties beyond the "closed" field, but even with that in mind, it's worth trying since regular single close() calls will keep working fine... I'm not expecting any regressions there.