版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shaohe18362202126/article/details/82814207
本章重点在于Spring在WEB应用中的使用原理,不涉及SSM框架的集成
一、Spring 如何在 WEB 应用中使用
1、创建一个WEB应用
2、在WEB应用中创建IOC容器
使用web应用的监听器,在 WEB 应用被服务器加载时就创建 IOC 容器。
(1)创建spring配置文件applicationContext.xml
配置bean
(2)配置文件到web应用的参数中
在web.xml中配置
<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
<param-name>configLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
(3)在监听器中创建IOC容器
*创建的IOC容器实例放在ServletContext(即 application 域)的属性中。
/**
* Application Lifecycle Listener implementation class
* SpringServletContextListener
*/
public class SpringServletContextListener implements ServletContextListener {
/**
* Default constructor.
*/
public SpringServletContextListener() {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
// 1. 获取 Spring 配置文件的名称.
ServletContext servletContext = arg0.getServletContext();
String config = servletContext.getInitParameter("configLocation");
// 1. 创建 IOC 容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
// 2. 把 IOC 容器放在 ServletContext 的一个属性中.
servletContext.setAttribute("ApplicationContext", ctx);
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
}
web.xml中注册监听器
<listener>
<listener-class>com.shao.spring.listener.SpringServletContextListener</listener-class>
</listener>
3、在servlet中访问ApplicationContext实例
(1)创建servlet
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1. 从 application 域对象中得到 IOC 容器的引用
ServletContext servletContext = getServletContext();
ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("ApplicationContext");
// 2. 从 IOC 容器中得到需要的 bean
Person person = ctx.getBean(Person.class);
person.hello();
}
}
(2)web.xml中配置servlet
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.shao.spring.servlet.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
4、测试
jsp页面进行测试
二、使用Spring提供的ContextLoaderListener
1、web.xml中注册ContextLoaderListener
*不使用上方自己写的Listener
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2、在Servlet中使用ApplicationContext的实例
public class TestServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1. 从 appication 域对象中得到 IOC 容器的实例
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
// 2. 从 IOC 容器中得到 bean
Person person = ctx.getBean(Person.class);
// 3. 使用 bean
person.hello();
}
}