Spring环境搭建

Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架.它的主要目的是简化企业开发。

 

搭建环境步骤:

1、到http://www.springsource.org/download下载spring,我用的是spring-framework-2.5.6的版本

 

2、导入2个常用包

     spring-framework-2.5.6\dist\spring.jar
     spring-framework-2.5.6\lib\jakarta-commons\commons-logging

 

3、创建spring.xml文件(该配置模版可以从spring的参考手册或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-2.5.xsd">  
           
</beans>  

 

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

 

     方法一:
     在类路径下寻找配置文件来实例化容器
     ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});

 

     方法二:
     在文件系统路径下寻找配置文件来实例化容器
     ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“d:\\beans.xml“});

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

 

5.编写测试类,测试Spring环境是否搭建成功

package org.spring;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
	@Test
	public void test() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"spring.xml");
		
	}
}

 

6、控制台信息

 控制台没有出现错误信息,环境搭建成功

 

7、工程目录结构图

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值