spring环境搭建

来源:互联网 发布:腾亿网络 编辑:程序博客网 时间:2024/05/16 14:02

1. 下载spring-framework-3.1.1.RELEASE

2. myeclips下创建web工程

3.  修改Web.xml文件,在其中加入spring的监听和配置文件加载信息。

   <context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring*.xml</param-value>   </context-param><listener>     <listener-class>org.springframework.web.context.ContextLoaderListener     </listener-class></listener>
4. 将spring的dist目录下的jar包导入到工程的WEB-INF的lib下。
5.配置应用程序上下文配置文件。
6. 在程序中通过以下方法加载上下文,

(1)在类路径下寻找配置文件来实例化容器

ApplicationContext ctx = new ClassPathXmlApplicationXContext(new String[]{“beans.xml”})

(2)在文件系统路径下寻找配置文件来实例化容器

ApplicationContext ctx = new FileSystemXmlApplication(new String[]{“d:\\beans.xml”});

Spring 的配置文件可以指定多个,可以通过String数组传入,也可以是一个String

  ctx.getBean()获取配置的bean实例。