搭建与测试Spring开发环境

南笙酒味 提交于 2019-12-06 20:09:16

1、Spring需要的jar

http://www.springsource.org/download下载spring,然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下。

spring.jar

commons-logging.jar

如果使用了切面编程(AOP),还需要下列jar文件

aspectjweaver.jar

aspectjrt.jar

如果使用了JSR-250中的注解,如@Resource/@PostConstruce/@PreDestroy,还需要下列jar文件

common-annotations.jar

2、Spring的配置文件模版

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring/beans/spring-beans-2.5.xsd">
</beans>

该配置模版可以从Spring的参考手册或Spring的例子中得到。配置文件的取名可以任意,文件可以存放在任何目录下,但考虑到通用性,一般放在类路径下。

3、实例化Spring容器

实例化spring容器常用的两种方式:

方法一:

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

ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});

方法二:

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

ApplicationContext ctx=new FileSystemXmlApplicationContext(new String[]{"d://beans.xml"});

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

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!