本文内容纲要:Spring学习一:搭建和测试spring开发环境
一:搭建开发环境
1、下载spring相关的架包,这个可以在网上搜索下载,可以参考
http://blog.csdn.net/undergrowth/article/details/9928377
2、创建web应用
3、我这里下载的是:spring-framework-m2-dist.zip、spring-framework-3.0.1.RELEASE-dependencies.zip
两个压缩包,将他们解压分别得到spring-framework-4.0.0.M2、spring-framework-3.0.1.RELEASE-dependencies 文件夹,然后将spring-framework-4.0.0.M2\libs 中的spring-beans-4.0.0.M2.jar、
spring-context-4.0.0.M2.jar、spring-core-4.0.0.M2.jar 三个jar copy出来放到web应用中的lib 文件夹先下,
再复制出spring-framework-3.0.1.RELEASE-dependencies\org.apache.commons\
com.springsource.org.apache.commons.logging\1.1.1 文件夹中的com.springsource.org.apache.commons.logging-1.1.1.jar ,同样式放到lib 文件夹下,如下图所示:
二:测试开发环境
1、在src下创建spring 配置文件 learn1.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">
</beans>
2、实例化spring容器,测试环境是否搭建成功,进行单元测试,创建单元测试类右键src----->New----->other如下图所示:
点击----> Next,如下图:
点击------> Finish,弹出如下图:
点击-------> OK。这时会在Libraries 中会自动导入JUnit 的jar。
类的代码如下:(为了更贴切,改了方法名)
package junit.test;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
@Test
public void initContainerSpring() {
//实例化spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("learn1.xml");
}
}
3、运行测试
选择创建的类,右键------>Run As------>JUnit Test ;控制台 Console 没有报错,但是 JUnit 会有如下错误:
我们在Libraries 中的包中没有找到这个类,从以上报的错误中我们可以知道这个类是来自spring 中的,那么我们到spring-framework-4.0.0.M2\libs 找,最终我们找到了 spring-expression-4.0.0.M2.jar 这个jar,将这个jar 放到WEB-INF\lib 中,刷新应用,重新运行程序不在报错,说明环境搭建成功。
三:spring注入
1、创建业务层类,名为PresonServiceImpl,代码如下:
package com.learn.service.impl;
import com.learn.service.PresonService;
/**
* 业务层
* @author Administrator
*
*/
public class PresonServiceImpl implements PresonService {
/* (non-Javadoc)
* @see com.learn.service.impl.PresonService#save()
*/
@Override
public void save(){
System.out.println("保存! ");
}
}
2、创建接口
面向接口编程,我们要创建一个业务层的接口,名称为PresonService,通过PresonServiceImpl 实现implements 。(后面会具体说明,如何从类中抽取接口,这里不细讲)
3、注入,向learn1.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">
<!-- id属性值是唯一的,id属性本身就是属于xml的属性,所以会受到xml解析器的验证,
它的值不能够包含特殊字符,例如:/;但是有时候我们需要用到特殊字符,所以我们就要用到name属性 -->
<!-- 当完成如下时,spring就会帮我们维护和创建该类;当我们要用这个类的时候,我们只需要从spring容器中去获取 -->
<bean id="personService" name="" class="com.learn.service.impl.PresonServiceImpl"></bean>
</beans>
4、然后单元测试,检测是否注入成功。在TestSpring 类中添加代码,代码如下:
@Test
public void initContainerSpring() {
//实例化spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("learn1.xml");
//从spring容器中获取bean getBean("personService")字符串要与spring配置文件learn1.xml属性id的值一致
PresonService personService = (PresonService)ctx.getBean("personService");
//调用save()
personService.save();
}
5、运行测试,控制台 Console 会打印出 “保存”,说明spring注入成功。
那么,我们可以接下来做开发。
四:部分内容 和 eclipse 工具的部分使用功能
1、如果learn1.xml 文件不是直接隶属在src 下,而是在scr 的包中,我们该如何实例化spring容器,例如:
如果learn1.xml 文件在如下图所示的包下:
那么,我们应该将:
//实例化spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("learn1.xml");
改为:
//实例化spring容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("com//learn//spring//learn1.xml");
2、如何从类中抽取接口:
右键(要抽取接口的类)------->Refactor-------->Extract Interface,如下图所示:
点击 OK 。
3、将创建好的接口放到,你想要放到的包下(也可以不放,看个人编程习惯):
右键(要抽取接口的类)------->Refactor-------->Move ,如下图所示:
选择包。点击 OK 。
本文内容总结:Spring学习一:搭建和测试spring开发环境
原文链接:https://www.cnblogs.com/hwlsniper/p/3436299.html