# SpringIntegrateWeb **Repository Path**: shieldofzues_admin/WebSpring ## Basic Information - **Project Name**: SpringIntegrateWeb - **Description**: 这个练习演示了如何在 Web 环境中使用 Spring 的 IOC 容器。思路其实很简单:通过重写 ServletContextListener 监听器的 contextInitialized 方法,将 Spring 的 IOC 容器放入 ServletContext 的一个属性中。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2017-11-06 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 使用 Spring 集成到 Web 环境(在 Web 环境中使用 Spring 管理所有的 bean) [TOC] 这是个简单的 Spring 集成到 Web 项目的练习。就是普通的 Web 项目 + Spring ,没有集成 SpringMVC 。 首先我们考虑 Spring 如何在 Web 项目中使用。 ## 一、Spring 如何在 Web 应用中使用 ? ### 1、需要额外加入的 jar 包 spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEASE.jar 【说明】我的实验证明 webmvc 这个包可以不加。 ### 2、Spring 的配置文件 bean.xml 这个文件和我们平常练习使用的 Spring 核心配置文件没有什么不同。 ### 3、如何创建 IOC 容器 ? ####(1)非 WEB 应用在 main 方法中直接创建; (这是我们非常熟悉的) ``` public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 默认调用无参数的构造函数 User user = (User) ctx.getBean("user"); System.out.println(user.getUsername()); System.out.println(user.getPassword()); } ``` #### 【重点】(2)应该在 Web 应用被服务器加载时就创建 IOC 容器( Web 应用被服务器加载时这个时机我们想要抓住,就要利用监听器的知识): 在 `ServletContextListener` 的 `contextInitialized(ServletContextEvent sce)` 方法中创建 IOC 容器。 示例代码: ``` public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println(" Web 容器启动的时候监听的方法。。。"); // 1、 获取 Spring 配置文件的名称 ServletContext servletContext = servletContextEvent.getServletContext(); String configLocation = servletContext.getInitParameter("configLocation"); System.out.println("configLocation " + configLocation); // 2、创建 IOC 容器(与在 main 方法中启动 Spring 的 IOC 容器没有什么不同) ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation); // 3、把 IOC 容器放到 ServletContext 的一个属性中(这个属性可以是我们自定义的属性) servletContext.setAttribute("ApplicationContext",ctx); } ``` #### 【重要】(3)接下来我们要解决的是在 Web 应用的其他组件中如何访问 IOC 容器呢 ? 在 `ServletContextListener` 重写的 `contextInitialized(ServletContextEvent sce)` 方法中创建 IOC 容器后, 可以把其放在 `ServletContext` (即 application 域)的一个属性中; 代码片段: ``` protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("Servlet 是单例还是多例呢? "); /** * 【实验证明】: Servlet 的构造器在第 1 次 访问的时候创建 * 后续再调用这个请求的时候不再实例化 * 【这个结论很重要】 */ int hashCode = this.hashCode(); System.out.println("this.hashCode() " + hashCode); // super.doGet(req, resp); //1、 从 application 域对象中得到 IOC 容器的引用 ServletContext servletContext = getServletContext(); ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("ApplicationContext"); //2、 从 IOC 容器中得到需要的 bean User user = (User) ctx.getBean("user"); System.out.println("Web 项目集成 Spring 要配置监听器。。。"); System.out.println(user.getUsername()); System.out.println(user.getPassword()); } ``` ``` helloServlet com.liwei.servlet.HelloServlet helloServlet /hello ``` ####(4)实际上, Spring 配置文件的名字和位置应该也是可配置的! 将其配置到当前 WEB 应用的初始化参数中较为合适。 代码片段: ``` configLocation classpath:beans.xml com.liwei.web.listener.SpringWebServletContextListener ``` 思考:我们经常做的 SpringMVC 、Spring 集成的时候,是不是有添加一个监听器呢?我们来看看这个监听器的配置。 ``` contextConfigLocation classpath:beans.xml org.springframework.web.context.ContextLoaderListener ``` 测试链接:http://localhost:8080/hello