一、Spring须要的jarspring
到http://www.springsource.org/download下载spring,而后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下。编程
spring.jar数组
commons-logging.jarxml
若是使用了切面编程(AOP),还须要下列jar文件get
aspectjweaver.jario
aspectjrt.jar容器
若是使用了JSR-250中的注解,如@Resource/@PostConstruce/@PreDestroy,还须要下列jar文件配置
common-annotations.jarcoding
二、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的例子中获得。配置文件的取名能够任意,文件能够存放在任何目录下,但考虑到通用性,通常放在类路径下。
三、实例化Spring容器
实例化spring容器经常使用的两种方式:
方法一:
在类路径下寻找配置文件来实例化容器
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
方法二:
在文件系统路径下寻找配置文件来实例化容器
ApplicationContext ctx=new FileSystemXmlApplicationContext(new String[]{"d://beans.xml"});
Spring的配置文件能够指定多个,能够经过String数组传入。