Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

前端 未结 6 676
挽巷
挽巷 2020-11-22 01:35
  • Are applicationContext.xml and spring-servlet.xml related anyhow in Spring Framework?
  • Will the properties files declared in
6条回答
  •  面向向阳花
    2020-11-22 01:46

    Scenario 1

    In client application (application is not web application, e.g may be swing app)

    private static ApplicationContext context = new  ClassPathXmlApplicationContext("test-client.xml");
    
    context.getBean(name);
    

    No need of web.xml. ApplicationContext as container for getting bean service. No need for web server container. In test-client.xml there can be Simple bean with no remoting, bean with remoting.

    Conclusion: In Scenario 1 applicationContext and DispatcherServlet are not related.

    Scenario 2

    In a server application (application deployed in server e.g Tomcat). Accessed service via remoting from client program (e.g Swing app)

    Define listener in web.xml

    
        org.springframework.web.context.ContextLoaderListener
    
    

    At server startup ContextLoaderListener instantiates beans defined in applicationContext.xml.

    Assuming you have defined the following in applicationContext.xml:

    
    
    
    
    

    The beans are instantiated from all four configuration files test1.xml, test2.xml, test3.xml, test4.xml.

    Conclusion: In Scenario 2 applicationContext and DispatcherServlet are not related.

    Scenario 3

    In a web application with spring MVC.

    In web.xml define:

    
        springweb
        org.springframework.web.servlet.DispatcherServlet    
    
    
    
        springweb
        *.action
    
    

    When Tomcat starts, beans defined in springweb-servlet.xml are instantiated. DispatcherServlet extends FrameworkServlet. In FrameworkServlet bean instantiation takes place for springweb . In our case springweb is FrameworkServlet.

    Conclusion: In Scenario 3 applicationContext and DispatcherServlet are not related.

    Scenario 4

    In web application with spring MVC. springweb-servlet.xml for servlet and applicationContext.xml for accessing the business service within the server program or for accessing DB service in another server program.

    In web.xml the following are defined:

    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        springweb
        org.springframework.web.servlet.DispatcherServlet        
    
    
    
        springweb
        *.action
    
    

    At server startup, ContextLoaderListener instantiates beans defined in applicationContext.xml; assuming you have declared herein:

    
    
    
    
    

    The beans are all instantiated from all four test1.xml, test2.xml, test3.xml, test4.xml. After the completion of bean instantiation defined in applicationContext.xml, beans defined in springweb-servlet.xml are instantiated.

    So the instantiation order is: the root (application context), then FrameworkServlet.

    Now it should be clear why they are important in which scenario.

提交回复
热议问题