servletcontext summary

ServletContext object: Each Servlet configuration information can be obtained through the web application ServletContext object, but also from the side that represents the ServletContext this web application, which is available through the servlet configuration information really a web application

Function: tomcat every web project to create a ServletContext instance, tomcat created at startup, destroyed when the server is shut down, the shared data in a web project, web project management resources, configuration information for the public as the entire web, popular speak, is a web project, there is a ServletContext instance, each Servlet can access to read it.

Web container when it starts to create a web application for each ServletContext object, and this ServletContext object on behalf of the current web application. Because a ServletContext object represents a web application, so that all the Servlet web applications and other resources all share a ServletContext object, then, we can communicate between objects through Servlet ServletContext object. And also called Context ServletContext object domain objects. 

This is a very important Servlet class. Of course ServletContext object may also be obtained from the parent class directly Obtaining: getServletContext ();, getServletConfig () getServletContext (); there are other ways of obtaining.

That is as long as we got ServletContext object by reading the object, can get the appropriate configuration information to the next step, the spring such as the bean obtained, by reading the configuration file <bean>

There are three ways you can get (spring projects get ApplicationContext object, and then manually obtain bean):

ApplicationContext description: If Spring BeanFactory is the heart, then the ApplicationContext is a complete body. Derived from the ApplicationContext BeanFactory it provides more features for practical application

Spring context (ApplicationContext) acquisition in three ways.

   1. Get through WebApplicationUtils tools. WebApplicationUtils class is class. RELEASE.jar (I'm using version 3.2.0 jar package, you can go to spring official website to download the latest version of the jar) in the Spring Framework base package spring-web-3.2.0. Using this method must rely Servlet container. Use as follows: 

ApplicationContext ap =WebApplicationUtils.getWebApplicationContext(servletContextParam)
How to get specific the bean: 
UserService US ap.getBean = ( "UserserviceId", Userservice.class)
or:
UserService US = (UserService) ap.getBean ( "UserserviceId") which is cast wording

 

     ServletContextParam which you need to pass Servlet container parameters.

     2. Get through ClassPathXmlApplicationContext class. ClassPathXmlApplicationContext class is class. RELEASE.jar (I'm using version 3.2.0 jar package, you can go to spring official website to download the latest version of the jar) in the Spring Framework base package spring-context-3.2.0. Use as follows:

ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml");


     Wherein applicationContext.xml files in src below, thus ensuring that the file to be read. The role of XML is centrally configure and manage all Bean.

    applicationContext.xml Code: 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http : //www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd -3.0.xsd " > 
        // middle part is all your own configuration the bean 
</ Beans >

 



Guess you like

Origin www.cnblogs.com/cherishforchen/p/10937624.html