一、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數組傳入。