《Spring 的在WEB 工程中加载.doc》由会员分享,可在线阅读,更多相关《Spring 的在WEB 工程中加载.doc(8页珍藏版)》请在金锄头文库上搜索。
1、Spring的在WEB工程中加载(2009-03-23 14:08:32) 标签: it分类: spring 在Web 工程中启动Spring 2.0的三种方法:(一)利用Spring 自带的Listener(推荐)-org.springframework.web.context.ContextLoaderListener=配置如下:在web.xml中配置spring框架所需要的配置内容contextConfigLocation-固定的内容/WEB-INF/classes/spring.xml-创建的spring的配置文件的路径 org.springframework.web.context.
2、ContextLoaderListener 配置完毕,可以启动 spring 容器了!=(二) 利用Spring 自带的 servlet-org.springframework.web.context.ContextLoaderServlet 配置如下: 在web.xml中配置spring 框架所需要的配置文件 contextConfigLocation-固定的内容 /WEB-INF/classes/spring.xml-创建spring的配置文件的路径context-固定的内容 org.springframework.web.context.ContextLoaderServlet1配置完毕
3、,可以启动 spring 容器了!可见上面两种方法来启动Spring都需要配置元素,来指定spring 的配置文件的位置。=(三) 利用第三方的WEB框架定义的 如:struts框架=配置如下:在Struts的配置文件中配置文配置完毕,可以启动 spring 容器了!Spring 有连个核心接口: BeanFactory: BeanFactory 是Spring的最基本的接口, ApplicationContext:BeanFactory的子接口,因此也成为Spring的上下文对象。很多时候不使用BeanFactory,而使用ApplicationContext的实例对象,他增强了BeanFa
4、ctory的功能。 BeanFactory 的实现类很多,主要有: (1) org.springframework. beans.factory.xml.XmlBeanFactory 类; (2) ApplicationContext(子接口),其实现类有: org.springframework.context.support.FileSystemXmlApplicationContext; org.springframework.context.support.ClassPathXmlApplicationContext。(一般使用这个实现类) 代码片段 (1):-定义数据源bean,利用
5、c3p0数据源实现 代码片段 (2):-定义List 、Map、Set、Properties 类型的集合元素 原始类: package com.haohaibo.doamain; public class Person private List list =new ArrayList(); private Map map =new HashMap();private Set set =new HashSet(); private Properties property=new Properties(); 配置文件: =List !- 使用元素来定义list对象的属性 - 张三1 张三2 张三3 张三4 =set !- 使用元素来定义set对象的属性,其值可以是任何形式的 - 普通String值属性 =Map !- 使用元素来定义map对象的属性 - 20 =properties !- 使用元素来定义properties对象的属性 - oracl.jdbc.driver.OracleDriver system =