Spring学习笔记02---搭建开发环境

一. Spring开发需要的jar包:

**下载最新的spring3.2.4开发包,百度网盘地址。当然你也可以访问http://repo.spring.io/release/org/springframework/spring/3.2.4.RELEASE/ 在官方网站下载。

** 下载之后解压如下:
解压路径
其中,docs目录是Spring的开发文档;
libs是Spring开发的jar包;
schema是一些xsd文件,我们可以通过导入这些文件来配置xml文件。

**打开libs目录,你可以发现有许多jar包,这里我们需要的有如下几个:
需要导入的jar包

二. 创建项目并导入Spring的jar包

创建一个Web项目(或者Java项目也可以),并导入上述的jar包,项目结构如下:
创建项目

我们可能使用到Junit4进行单元测试,所以导入Junit4的jar包。点击项目右键–>buildpath–>config build path–>library –>add library,并选择junit选项即可:
导入junit4

三. 创建Spring的配置文件

** spring的配置文件是一个XML文件,它的名称可以任意取,但是必须在resource目录或者其子目录下。同时我们需要得到这个配置文件的配置模板,配置模板如下:

<?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.xsd"> 
</beans

这段配置模板,你可以从spring的参考手册或者例子中得到,具体如下:

1。打开下载的spring目录,并找到开发文档并打开:
找到index.html

2。打开index.html,搜索beans,即可得到spring的配置模板:
这里写图片描述

我给配置文件取名为beans.xml,并将配置模板拷贝进入即可:
这里写图片描述

四. 实例化spring容器。

实例化spring容器有两种方法:
第一种,在类路径下寻找配置文件来实例化容器

 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

第二种,在文件系统路径下寻找配置文件

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

需要说明的是,如果有多个配置文件,可以传入一个String数组,如果只有一个配置文件,则只需要传入文件名称的字符串。其次,如果配置文件不在resource根目录下,而是在其子目录下,则需要将子目录也添加到路径里面,比如“config/beans.xml”,说明这个beans.xml配置文件在 res/config/ 目录下面。

这个时候,我们可以写一个测试类来测试spring容器是否初始化成功,新建junit.test包并右键 new –>junit–>junit Test Case 并取名为SpringTest.java,具体代码如下:

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

import cn.itcast.service.IPersonService;

public class SpringTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @Test
    public void instanceSpring(){
        //初始化Spring容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

    }

}

最后,右键这个类,run as –>junit test 如果打印如下信息,说明spring容器初始化成功:
打印信息

五. 创建业务bean并交给spring容器来维护。

前面介绍控制反转(Inversion of Control)时我们提到,IOC就是将依赖对象的创建以及维护交给外部容器,这里的外部容器就是spring。
这里我们创建一个简单的业务bean,并交给spring容器管理。
创建如下的包和类:
创建业务bean

PersonServiceBean .java:


package cn.itcast.service.impl;

import cn.itcast.service.IPersonService;

public class PersonServiceBean implements IPersonService {
   /* (non-Javadoc)
 * @see cn.itcast.service.impl.IPersonService#save()
 */
@Override
public void save(){
       System.out.println("我是save()方法!");
   }
}

IPersonService.java

package cn.itcast.service;

public interface IPersonService {

    void save();

}

这个业务bean很简单,里面只有一个save方法,我们需要在beans.xml中配置这个业务bean。
在beans.xml中如下配置:

<?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.xsd">

   <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>

</beans>

注意,id在整个spring容器中是唯一的,不能重复取,class属性里面传入的是类名全路径。

创建好了之后,在测试类中通过 ctx.getBean()方法即可获得这个业务bean,并能调用它的save方法:

@Test
    public void instanceSpring(){
        //初始化Spring容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        IPersonService personService = (IPersonService) ctx.getBean("personService");
        personService.save();
    }

测试运行结果如下:
测试结果
这说明我们成功的初始化了一个spring容器,并通过它来管理我们的业务bean。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值