The difference between the web context (Servlet contexts), the spring context (WebApplication contexts), and the SPRINGMVC context (Mlwebapplicationcont).
Context: Can be easily understood as a container, configuration file
The web context target object is all Web apps, and the spring context target object is a single web app, and the spring MVC target object is a spring MVC framework for a single web app (the child context of the spring context, which is inherited from the spring context, So the child is able to invoke the parent's things, and vice versa.
Above feel oneself understand have deviation, see a good, first Markdow
Original link
First, say ServletContext
The Java EE standard specifies that the servlet container needs to initialize a servletcontext as a public environment container for public information when the application project starts. The information in the ServletContext is provided by the container.
Example:
Get the parameters configured in Web. XML by customizing Contextlistener 1. When the container starts, locate the Context-param in the configuration file as the key-value pair in ServletContext 2. Then find Listener, The container invokes its contextinitialized (Servletcontextevent event) method, performing operations such as: Configuring in Web. xml
< context-param> <param-name> Key</param-name> <param-value>value123</param-value></context-param>< listener> <listener-class> Com.brolanda.contextlistener.listener.contextlistenertest</ Listener-class></LISTENER>
Once configured, get the corresponding parameter information in the class
Package Com.brolanda.contextlistener.listener;Import Javax.servlet.ServletContext;Import javax.servlet.ServletContextEvent;Import Javax.servlet.ServletContextListener;PublicClasscontextlistenertest implements Servletcontextlistener {public void contextdestroyed (Servletcontextevent event) {System.out.println ( ' *************destroy contextlistener************* '); } @SuppressWarnings ( ' unused ') public void contextInitialized ( Servletcontextevent event) {System.out.println ( ' *************init contextlistener****** ‘); ServletContext ServletContext = Event.getservletcontext (); System.out.println ( ' key: ' +servletcontext.getinitparameter ( ' key '); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
Execution process:
Web. XML declares the application-scoped initialization parameters in the label
1. When you start a Web project, the container (for example, Tomcat) reads its configuration file, Web. Read two nodes: and 2. Immediately thereafter, the container creates a ServletContext (context). Globally shared within the app.
3. The container will be converted to a key-value pair and handed to ServletContext.
4. Create a listener for the class instance in the container creation. The listener must implement a self-servletcontextlistener interface
5. Contextinitialized (Servletcontextevent Event) initialization method in listening
Get ServletContext = Servletcontextevent.getservletcontext () in this method;
"Value of Context-param" = Servletcontext.getinitparameter (' Context-param key '); 6. After you get the value of this context-param, you can do something about it. Note that this time your Web project is not fully started yet. This action will be earlier than all servlets. In other words, this time, you do the key value in the operation, will be executed before your Web project is fully booted. There are two parameters that can be defined in Web. xml: One is the global parameter (ServletContext), and through the servlet parameter, the first parameter is declared in the servlet by param1 avalible in servlet init () Servle The second parameter can be obtained by Getservletcontext (). Getinitparameter (' Context/param ') in the servlet's init () method only through This.getinitparameter (' param1 ') obtained
Second, Spring context container configuration
Spring provides us with a context-initialization listener that implements the Servletcontextlistener interface: Org.springframework.web.context.ContextLoaderListener
Spring provides us with an IOC container that requires us to specify the container's configuration file, which is then initialized and created by the listener. Requires you to specify the address and file name of the configuration file, be sure to use: Contextconfiglocation as the parameter name.
<context-param> <param-name>contextconfiglocation</param-name > <param-value>/web-inf/applicationcontext.xml,/ Web-inf/action-servlet.xml,/web-inf/jason-servlet.xml</ Param-value></context-param> <listener> <listener-class >org.springframework.web.context.contextloaderlistener</ Listener-class></LISTENER>
The listener, by default, reads the Applicationcontext.xml file under/web-inf/. However, after specifying the configuration file path through Context-param, the corresponding configuration file is read and initialized under the path you specify.
Iii. what is initialized after the spring context container is configured?
Now that ServletContext was initialized by the servlet container, what did spring Contextloaderlistener do to initialize it?
1, the servlet container starts, creates a "global context" for the application: ServletContext 2, The container invokes the Contextloaderlistener configured in Web. XML, initializes the Webapplicationcontext context (that is, the IOC container), loads the Context-param specified profile information into the IOC container. Webapplicationcontext is stored as a key-value pair in the ServletContext 3, The container initializes the servlet configured in Web. XML, initializes its own context information servletcontext, and loads the configuration information for its settings into that context. Sets the Webapplicationcontext to its parent container. 4. All subsequent servlet initializations are created in 3 steps, initializing their own context and setting Webapplicationcontext to their parent context.
For the scope, the contents of the ApplicationContext created by Contextloaderlistener can be referenced in Dispatcherservlet, but not in the reverse. When spring executes ApplicationContext's Getbean, it is found in the parent applicationcontext if the corresponding bean is not found in its own context. This also explains why we can get the beans in the dispatcherservlet that correspond to the ApplicationContext in the Contextloaderlistener.
Four, spring configuration: The use of reason, why the controller is excluded in Applicationcontext.xml, and in Spring-mvc.xml Incloud this controller
Now that you know the spring START process, the Web container initializes the Webapplicationcontext as a public context, only the configuration information for service, DAO, and so on is loaded here, and the servlet's own contextual information does not need to be loaded. Therefore, the components of the @controller annotation are excluded from the applicationcontext.xml, and the components of the @controller annotation are loaded in the Dispatcherservlet loaded configuration file. Easy to Dispatcherservlet control and find. Therefore, the configuration is as follows: APPLICATIONCONTEXT.MXL:
<Context:component-scanBase-package=' Com.linkage.edumanage ' ><Context:exclude-filterexpression=' Org.springframework.stereotype.Controller ' type=' annotation '/> </Context: Component-scan> spring-mvc.xml: <context:component-scan base-package=' Com.brolanda.cloud ' use-default-filters=' false ' > <context:include-filter expression=' Org.springframework.stereotype.Controller ' type=' annotation '/> </Context: component-scan>
1
2
Package Com.brolanda.contextlistener.listener;Import Javax.servlet.ServletContext;Import javax.servlet.ServletContextEvent;Import Javax.servlet.ServletContextListener;PublicClasscontextlistenertest implements Servletcontextlistener {public void contextdestroyed (Servletcontextevent event) {System.out.println ( ' *************destroy contextlistener************* '); } @SuppressWarnings ( ' unused ') public void contextInitialized ( Servletcontextevent event) {System.out.println ( ' *************init contextlistener****** ‘); ServletContext ServletContext = Event.getservletcontext (); System.out.println ( ' key: ' +servletcontext.getinitparameter ( ' key ');
Small knowledge:
1. Why add use-default-filters= ' false ' to the Spring-mvc.xml filter.
Answer: Because use-default-filters is used to indicate whether to automatically scan with
Classes @Component, @Repository, @Service, and @controller. The default is true, which is the default scan. And we asked to just scan the controller.
RELATED LINKS
2. Why the sweep package in spring removes only the controller in controller,spring MVC (pring MVC management controller,spring The bean outside the management controller).
A: This is related to spring MVC's workflow, the main spring MVC Bean is the controller class that implements the Controller interface. Look at the picture:
Where handler mapping is a xxx-servlet.xml file, this is the Dispatchservlet context (note is the Dispatchservlet) configuration file, which is configured for the different URL is the request corresponding controller.
Related JSP learning recommended JSP programming for people's post and Telecommunications publishing house (page 230 has spring MVC content).
But I still have a question, since the context of Spring MVC inherits the context of spring, then spring MVC does not seem to have a problem with all the packages, only the bean inherited to the parent class is overwritten, to be resolved.
Web context in spring, spring context, SPRINGMVC context difference (super verbose)