1、Spring 如何在WEB应用中使用 ?
1). 需要额外加入的 jar 包:
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
2). Spring 的配置文件, 没有什么不同
3). 如何创建 IOC 容器 ?
①. 非 WEB应用情况下,在main方法中直接创建
②. web应用情况下,应该在WEB应用被服务器加载时就创建IOC容器:
在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC容器.
③. 在 WEB 应用的其他组件中如何来访问 IOC 容器呢 ?
在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器后, 可以把其放在
ServletContext(即 application 域)的一个属性中.
④. 实际上, Spring 配置文件的名字和位置应该也是可配置的! 将其配置到当前 WEB 应用的初始化参数中较为合适.
根据以上思路完成Spring在Web中的应用:
(1)创建一个web工程spring_web,在webContent/WEB-INF/lib下导入jar包
(2)创建java类,并在spring的xml文件中配置该类的bean实例
(3)在web.xml中配置Spring的xml文件的名字和位置
(4)创建一个ServletContextListener接口的实现类SpringServletContextListener,用于获取IOC容器,并将IOC容器放在ServletContext(即 application 域)的一个属性中,以便Web应用的其它组件来访问IOC容器。
(5)创建一个Servlet类TestServlet,验证是否能成功访问到IOC容器
(6)创建一个index.jsp,用于测试验证结果
(7)在web.xml中配置监听器及Servlet类
(8)将web项目部署在Tomcat服务器上,并运行该web项目。
2、在通用的Web应用中访问Spring
经过前面的讲述,相信大家对Spring在Web中应用的思路有了一定的了解。不过在实际中,并不需要我们自己去实现一个ServletContextListener监听类,Spring提供了一个ContextLoaderListener。通过注册Servlet 监听器 ContextLoaderListener, Web应用程序可以加载Spring的ApplicationContext对象。这个监听器会将加载好的ApplicationContext对象保存到Web应用程序的 ServletContext 中。随后,Servlet或可以访问ServletContext的任意对象就能通过一个辅助方法来访问Spring的应用程序上下文(IOC容器)了。
在web应用程序中访问Spring ApplicationContext对象:可以通过的静态方法来获取Spring的ApplicationContext对象。
根据以上思路完成Spring在Web中的应用:
(1)创建一个web工程spring_web1,在webContent/WEB-INF/lib下导入jar包
(2)创建java类,并在spring的xml文件中配置该类的bean实例
person.java:
public class Person {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void hello(){
System.out.println("My name is " + username);
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.sjk.spring.bean.Person">
<property name="username" value="Tom"></property>
</bean>
</beans>
(3)在web.xml中配置Spring的xml文件的名字和位置,以及ContextLoaderListener只需在web.xml中添加如下两句:
<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring-web1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
(4)创建一个test.jsp,测试结果
使用ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);获取IOC容器。
test.jsp:
<%@page import="com.sjk.spring.bean.Person"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//1. 从 appication 域对象中得到 IOC 容器的实例
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
//2. 从 IOC 容器中得到 bean
Person person = ctx.getBean(Person.class);
//3. 使用 bean
person.hello();
%>
</body>
</html>
结论:
4). 在 WEB 环境下使用 Spring ①. 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEASE.jar ②. Spring 的配置文件, 和非 WEB 环境没有什么不同 ③. 需要在 web.xml 文件中加入如下配置: <!-- 配置 Spring 配置文件的名称和位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 启动 IOC 容器的 ServletContextListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
本博客内容来自尚硅谷佟刚老师的课程—《Spring4》
本博客中的源码源码