博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring学习案例一:给项目添加spring框架
阅读量:794 次
发布时间:2019-03-25

本文共 958 字,大约阅读时间需要 3 分钟。

文章目录

1. Spring学习案例一:给项目添加spring框架

1.1. 前提条件

  1. 导包,spring环境的jar包,spring支持web工程的包
  2. 配置applicationContext.xml
  • 配置applicationContext.xml的目的是将dao实现层,service实现层,需要创建的对象交给spring来创建,将new实例去掉,添加set()方法,然后在applicationContext.xml中添加控制反转。
  1. 配置web.xml实现spring容器随着项目启动时启动,随着项目关闭时关闭。
org.springframework.web.context.ContextLoaderListener
  1. 指定加载spring配置文件的位置
contextConfigLocation
classpath:applicationContext.xml
  • spring 将容器放到了servletContext域中,即:Application域中。
  1. 获取ServletContext对象,从ServletContext对象中获取ApplicationContext.xml容器,再从applicationContext.xml中获取CustomerService。这样就不会请求一次创建一个容器了。
//1.	获取servletContext对象ServletContext sc=ServletActionContext.getServletContext();//2.	从sc中获取ac容器WebApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(sc);//3.	从容器中获取CustomerServiceCustomerService service=(CustomerService)ac.getBean("customerService");

转载地址:http://nduyk.baihongyu.com/

你可能感兴趣的文章