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. SIGTERM、SIGKILL、SIGINT和SIGQUIT的区别

    转自:http://blog.csdn.net/dai_xiangjun/article/details/41871647 SIGQUIT 在POSIX兼容的平台,SIGQUIT是其控制终端发送到进程 ...

  2. .net连接MySql 出错

    1>C#连接MySQL异常:The host localhost does not support SSL connections. 需要在连接字符串加入:SslMode = none;即可解决 ...

  3. Error:stray '\243' in program

    c++ 程序出现 Error:stray '\243' in program 错误 错误情况: 错误原因: 有不标准的 ASCII 字符出现,一般是中英文问题,比如 :or ;     , or ,等 ...

  4. oracle中job定时任务96

    .INTERVAL参数常用值示例 每天午夜12点            ''TRUNC(SYSDATE + 1)'' 每天早上8点30分         ''TRUNC(SYSDATE + 1) +  ...

  5. laravel中的模型关联之(一对多)

    一对多 一对多就相当于,一个用户有多篇文章,这多篇文章都对应一个用户 这是一张文章表,一个用户有多篇文章,这里是在用户模型里面获取用户的所有文章, 第二个参数就是获取的模型文章表(post)里面的用户 ...

  6. Reportviewer中的函数使用——打印当前日期并格式化

    如2017-10-23 12:20:20 通过DateTime.Now.ToString("yyMMddHHmmss")变为 20171023122020字符串

  7. JavaScript各种继承方式(四):原型式继承(prototypal inheritance)

    一 原理 利用工具函数,通过原型对象直接得到父类的实例,并当作子类对实例使用. function inherit(obj){ // 在闭包中创建子类,对外隐藏子类 function Temp(){ } ...

  8. word 2016 加载 mathtype

    1.加载wold: 首先打开word,选择选项-------加载项------管理(A),选择word加载项,点击转到 这儿显示的是我已经添加过的所以显示的有. 2.点击添加,找到你的mathtype ...

  9. f5 2017.09.03故障

    1.下午14点50左右有同事反应epm等系统登录有问题.自测登录也是有同样的报错. 2.测试发现内部IP直接访问正常,但是访问f5的vip的方式访问不了.此时oa.邮件等系统也开始有同事发现故障. 3 ...

  10. mysql学习1:数据类型:数字型,日期和时间,字符串类型(总结)

    mysql数据类型:数字型,日期和时间,字符串类型 摘要 MySQL中定义数据字段的类型对数据库的优化是非常重要的: MySQL支持多种类型,大致可以分为三类,如下. 数字类型 整数:tinyint. ...