Spring - application Initialized twice?

前端 未结 5 876
别那么骄傲
别那么骄傲 2020-12-09 19:57

When I starts to run my spring application my tomcat , the ContextRefreshedEvent fires twice. Please see the StackTrace.

Dec 20, 2013 6:07:56 PM         


        
5条回答
  •  感动是毒
    2020-12-09 20:44

    This seems normal as Spring MVC application have usually at least two contexts, see this answer.

    In this case this application has two different contexts that serve different purposes:

    • a global Spring context common to the whole application

    • a servlet-level spring context, which contains all the beans associated to a given dispatcher servlet.

    A Spring MVC application can have multiple dispatchers, each with it's own context and all sharing a parent context with common application-wide beans.

    In each dispatcher context there are beans specific of the dispatcher that cannot be injected in other contexts and neither on the parent context, for example all beans annotated with @Controller.

    This ensures that we can inject service and DAO beans defined in the common context in any controller, but we can never inject a controller into a service, or controllers/beans from one dispatcher into another dispatcher, keeping the dispatchers isolated.

    According to the log, the first context initialised is the root context, and the second context is the dispatcher context, which is OK.

提交回复
热议问题