Spring----学习(24)---在web应用中使用spring(原理)

1. spring在web中的使用步骤。

      1.1 jar包(必须的)

           spring-web-4.0.0.RELEASE.jar
           spring-webmvc-4.0.0.RELEASE.jar

     1.2 配置文件相同。

      1.3 创建IOC容器。

               》》》非web应用直接在main方法中创建

               》》》web应用应该在服务器加载的时候就创建IOC容器。

               》》》在ServletContextListener的contextInitialized(ServletContextEvent sce) 方法中创建IOC容器。

                          在创建之后,可以把其放在ServletContext(application域中)的一个属性。

               》》》实际上,spring的配置文件应该是可配置的。将其配置到当前web应用的初始化参数中较为合适。

  原理:1.准备好spring的Javabean和.xml配置文件

            2.在web.xml文件中将spring配置文件的位置信息以可修改参数的形式定义。(好处:如果在外部spring配置文件

                的名字发生改变只需要修改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_2_5.xsd" id="WebApp_ID" version="2.5">
  <context-param>
    <param-name>configlocation</param-name>
    <param-value>applicationContext.xml</param-value>
  </context-param>
</web-app>

         3.在listener中创建IOC容器并把它作为一个属性放入servlet中。

          (当程序加载的时候,自动调用到该listener时,在contextInitialized方法中如下创建IOC容器,

             并放入ServletContext中。这个时候在servlet中已经有IOC容器了。)

public class Listener implements ServletContextListener {
    public Listener() {
    }

    public void contextDestroyed(ServletContextEvent arg0)  {
    }

    public void contextInitialized(ServletContextEvent arg0)  {
    	//1.从ServletContext获取配置好的spring配置文件的信息。
        ServletContext servletContext = arg0.getServletContext();
        String config = servletContext.getInitParameter("configlocation");
    	//2.创建IOC容器
    	ApplicationContext app = new ClassPathXmlApplicationContext(config);
        //3.把IOC容器放到ServletContext的一个属性中
    	servletContext.setAttribute("ApplicationContext", app);
    }
}

      4. 测试调用IOC容器中的bean。(这个时候,从测试用的jsp页面发送请求到测试用的testservlet时,

          如下调用IOC容器中的bean)

public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
       //1.从application域对象中得到IOC容器的引用
	ServletContext servletContext = getServletContext();
	ApplicationContext app  = (ApplicationContext) servletContext.getAttribute("ApplicationContext");

	//2.从IOC容器中得到需要的bean
	Person person = app.getBean(Person.class);
	person.hello();
	}

}

5. 以上均为测试该原理使用代码。实际上在servlet中已经存在编写好的调用IOC容器的listener。(ContextLoaderListener)

     5.1 准备好spring的Javabean和.xml配置文件

  5.2 在web.xml文件中将spring配置文件的位置信息配好。(ServletContextListenger已经将如何创建IOC容器

      等工作完成。这样不用关注IOC容器是如何创建的了。)

<?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_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<!-- 配置spring配置文件的名称和位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- 启动IOC容器的ServletContextListenger -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

  5.3 测试用 (WebApplicationContextUtils.getWebApplicationContext)

<%@page import="com.lishenhan.beans.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);
		Person person = ctx.getBean(Person.class);//2. 从 IOC 容器中得到 bean
		person.hello();//3. 使用 bean
	%>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/lsh15846393847/article/details/89451790