spring和web项目进行整合,其实就是在项目启动时,就创建spring容器,然后在servlet中使用spring容器进行开。

注意:为了页面可以访问到servlet,因此servlet必须放进tomcat或者类似的服务器容器中,如果把servlet放进spring容器中,前端页面是无法访问的

第一步:导入spring-web.jar包,因为有一些别的依赖关系,还需要导入spring-tx.jar,spring-aop.jar等包

第二步:编写web.xml配置文件

  在web.xml配置一个ServletContext监听器,在项目一启动就执行该监听器,该监听就会执行创建spring容器的代码,这样就可以保证在web项目启动时就有spring容器。

  创建spring容器是需要配置文件的,因此要告诉监听器到哪里加载配置文件,加载配置文件的位置通过<context-param>进行配置。

<!-- 配置spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--
spring中配置了一个监听器
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
类org.springframework.web.context.ContextLoaderListener在spring-web.jar包中
源码如下,可以看到ContextLoaderListener 是实现了SerlvetContextListener接口的
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

    public ContextLoaderListener(WebApplicationContext context) {
super(context);
} /**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
} /**
* Close the root web application context.
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
} }

第三步:在web使用spring容器

@WebServlet("/airportServlet")
public class AirportServlet extends HttpServlet{ private static final long serialVersionUID = 1L;
private AirportService airportService; @Override
public void init() throws ServletException {
WebApplicationContext wa=null;
WebApplicationContext ac=WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
airportService=ac.getBean("airportService",AirportServiceImpl.class);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
List<Airport> airportList = airportService.showAirport();
for (Airport airport : airportList) {
System.out.println(airport);
}
} }

过程描述:

web项目启动时,ContextLoaderListener开始执行,ContextLoaderListener监听器会根据下面的配置读取spring的配置文件

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

然后根据配置文件内容创建一个WebApplicationContext类型的容器,这个容器是ApplicationContext的子接口,源码如下:

public interface WebApplicationContext extends ApplicationContext {

    String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";

    String SCOPE_REQUEST = "request";

    String SCOPE_SESSION = "session";

    String SCOPE_GLOBAL_SESSION = "globalSession";

    String SCOPE_APPLICATION = "application";

    String SERVLET_CONTEXT_BEAN_NAME = "servletContext";

    String CONTEXT_PARAMETERS_BEAN_NAME = "contextParameters";

    String CONTEXT_ATTRIBUTES_BEAN_NAME = "contextAttributes";

    ServletContext getServletContext();

}

最后可以在servlet中使用一个WebApplicationContextUtils的工具类获取WebApplicationContext容器,然后使用spring容器。

spring学习七 spring和dynamic project进行整合的更多相关文章

  1. Spring学习(七)--Spring MVC的高级技术

    一.Spring MVC配置的替代方案 我们已经了解如何通过AbstractAnnotationConfigDispatcherServlet- Initializer快速搭建了Spring MVC环 ...

  2. Spring学习(七)-----Spring Bean的5种作用域

    在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者.bean支持的5种范围域: 单例(singleton) - 每个Spring IoC 容器返回一 ...

  3. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  4. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

  5. Spring学习七----------Bean的配置之自动装配

    © 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...

  6. spring学习12 -Spring 框架模块以及面试常见问题注解等

    以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式—在AOP和remoting中被用的比较 ...

  7. Spring学习【Spring概述】

    从本文開始,我们就要一起学习Spring框架,首先不得不说Spring框架是一个优秀的开源框架. 当中採用IoC原理实现的基于Java Beans的配置管理和AOP的思想都是非常值得学习与使用的.以下 ...

  8. Spring学习笔记—Spring之旅

    1.Spring简介     Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...

  9. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

随机推荐

  1. Tomcat添加管理员role

       最近朋友问我怎么在Tomcat里面使用 admin 登录,一般情况下登录后是提示xxx的,经过百度后,好不容易才找到答案:    原来添加一个role为admin:<role rolena ...

  2. SQLdeveloper同时显示多个表的窗口

  3. leetcode 数组类型题

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h& ...

  4. python之列表、元组、字典学习

    list特征列表中的元素可以是数字和字符串,列表,布尔值,列表中也可以嵌套列表 a=[1,2,3,"qqq","无"] b=[1,2,3,[1,2,3,&quo ...

  5. 第十章 优先级队列 (b1)完全二叉堆:结构

  6. linux命令之----sort命令用于将文本文件内容加以排序

    1.sort命令作用 sort命令用于将文本文件内容加以排序,将输入行按照键值字段与数据类型选项以及locale排序. 一个可预期的记录次序,会让用户的查看使用更方便:书的索引.字典.目录以及电话簿等 ...

  7. PAT1135(红黑书的判定)

    There is a kind of balanced binary search tree named red-black tree in the data structure. It has th ...

  8. JavaScript各种继承方式(六):寄生组合继承

    一 原理 用寄生继承来改造组合继承. function Fruit(name){ this.name = name; } Fruit.prototype.eat = function(){ conso ...

  9. ECMAScript6的原型

    class Car { constructor(name){ this.name = name; } getName(){ } } class Ferrari extends Car{ constru ...

  10. 微信小程序开发——使用回调函数出现异常:TypeError: Cannot read property 'setData' of undefined

    关键技术点: 作用域问题——回调函数中的作用域已经脱离了调用函数了,因此需要在回调函数外边把this赋给一个新的变量才可以了. 业务需求: 微信小程序开发,业务逻辑需要,需要把获取手机号码的业务逻辑作 ...