時間:2017-2-2 02:17
——導入jar包
1、導入Spring開發基本jar包
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
2、導入commons-logging.jar
3、導入Spring Web開發jar包
spring-web-3.2.0.RELEASE.jar
——簡單測試
1、編寫一個Service
2、編寫一個Servlet
3、編寫配置文件
4、編寫log4j.properties
5、訪問Servlet調用Service方法
但是在測試的過程中發現:
每次執行Servlet的時候都會加載Spring環境,如何解決?
* 將加載的信息內容保存到ServletContext中,ServletContext對象是全局對象,服務器啟動時就會創建,在創建ServletContext時就會加載Spring環境。
* 可以創建一個監聽器:ServletContextListener,用於監聽ServletContext對象的創建和銷毀。
這件事情spring-web-3.2.0.RELEASE.jar幫助我們完成了。
——配置監聽器
將Spring容器的初始化操作,交由Web容器負責。
1、配置核心監聽器:ContextLoaderListener
Spring提供的ContextLoaderListener實現了ServletContextListener接口。
2、配置全局參數:contextConfigLocation
用於指定Spring框架的配置文件的位置。
默認在XmlWebApplicationContext類中指定為WEB-INF目錄下:
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
如果需要修改默認目錄,可以通過初始化參數進行修改:
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
——獲得WebApplicationContext對象
因為Spring容器已經交由Web容器初始化和管理,所以獲得WebApplicationContext對象需要依賴ServletContext對象:
通常直接在Servlet中獲取:
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
底層也是通過:getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);來獲得。
另一種獲取方式:
WebApplicationContext context = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
通常使用第一種方式來獲得ApplicationContext對象。
——示例代碼
Servlet:
public class UserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
UserService userService = (UserService) context.getBean("userService");
userService.sayHello();
}
}
----------------------------------------------------------------------------------------------------------------------------
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="
http://java.sun.com/xml/ns/javaee" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.wyc.servlet.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
----------------------------------------------------------------------------------------------------------------------------
spring配置文件:
<?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:context="
http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userService" class="com.wyc.service.UserService" />
</beans>
----------------------------------------------------------------------------------------------------------------------------
UserService:
public class UserService {
public void sayHello(){
System.out.println("Hello Spring Web");
}
}