spring学习笔记整理--02(搭建与测试Spring的开发环境)
使用Sping需要的jar:
到http://www.springsource.org/download下载spring,然后进行解压缩,寻找下面的jar文件。不过官方的需要注册,(ps:职业习惯,极度讨厌输入自己的电话号码和邮编,写邮箱已经是底线)在网上找到了一个链接,速度很好!很是感谢分享的人哪,以下链接是包含全部依赖关系的spring完整官方jar包70几M,如以后链接失效,留言时贴邮箱,免费散发!
(百度知道的链接,都贴上来了http://zhidao.baidu.com/question/99088542.html)
基本包:
dist\spring.jar
lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要下列jar文件:
lib\aspectj\aspect jweaver.jar和aspectjrt.jar
lib\cglib\cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注释,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件:
lib\j2ee\common-annotations.jar
一.建立一个java项目

二.导入spring的基本包

同上导入spring-framework-2.5.6\lib\jakarta-commons\commons-logging.jar然后点finish结束。
三.生成spring的xml文件
首先说明此文件模板在哪里:在spring-framework-2.5.6\docs\reference\html_single中找到index.html

模板代码如下:
<?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">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
在src根目录下建立beans.xml,贴入以上模板的代码(注:中间有可能会出现编码问题,个中bean最好重新手写),去掉中间所有的<bean><bean/>,仅仅给一个空实现。
四.建立一个junit测试类,来测试spring是否配置成功:
在根目录下建立一个Junit Test Case类,点击下面的Click here超级链接会弹出添加jar包的页面,直接点击ok!

然后点finish完成。
Spring02Test代码如下:
package junit.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Spring02Test {
@Test public void instanceSping(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
}
}
然后Run As Junit Test "instanceSping()"方法,通过。
五.离线提示spring标签设置
window->perfenerces->MyEclipse->Files And Editors->XML->XML CataLog
然后在User Specified Entries下Add,弹出选项卡:
Location:(选择File System)F:\myExamples\jar-file\spring-framework-2.5.6\dist\resources\spring-beans-2.5.xsd
Key Type:Schema Location
Key:http://www.springframework.org/schema/beans/spring-beans-2.5.xsd(红色部分别忘了添加上去)
然后点击OK完成。
六.新建一个简单的实现类和接口,由spring来管理,代码如下:
实现类:
package cn.service.impl;
import cn.service.PersonService;
public class PersonServiceImpl implements PersonService {
public void save(){
System.out.println("This is save()!");
}
}
-------------------------------------------------------------------
接口:
package cn.service;
public interface PersonService {
public abstract void save();
}
-------------------------------------------------------------------
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-2.5.xsd">
<bean id="personService" class="cn.service.impl.PersonServiceImpl"></bean>
</beans>
-------------------------------------------------------------------
junit 测试类:
package junit.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.service.PersonService;
public class Spring02Test {
@Test public void instanceSping(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService)ctx.getBean("personService");
personService.save();
}
}
七.spring读取xml实例化的原理
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");都是现有的接口和类,现在写个自己的ClassPathXmlApplicationContext类,取名叫TzClassPathXmlApplicationContext,代码如下:
(注:需要导入dom4j的包)
TzClassPathXmlApplicationContext :
package commons.tz;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
public class TzClassPathXmlApplicationContext {
private List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
private Map<String, Object> sigletons = new HashMap<String, Object>();
public TzClassPathXmlApplicationContext(String fileName) {
this.readXml(fileName);
this.instanceBeans();
}
/*
* 实例化一个bean对象
* */
private void instanceBeans() {
for (BeanDefinition beanDefinition : beanDefinitions) {
if (beanDefinition.getClassName() != null
&& !"".equals(beanDefinition.getClassName().trim())) {
try {
sigletons.put(beanDefinition.getId(), Class.forName(
beanDefinition.getClassName()).newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/*
* 读取xml文件
* */
private void readXml(String fileName) {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
URL xmlpath = this.getClass().getClassLoader()
.getResource(fileName);
document = saxReader.read(xmlpath);
Map<String, String> nsMap = new HashMap<String, String>();
nsMap.put("ns", "http://www.springframework.org/schema/beans");//加入命名空间
XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径
xsub.setNamespaceURIs(nsMap);//设置命名空间
List<Element> beans = xsub.selectNodes(document);//获取文档下所有bean节点
for (Element element : beans) {
String id = element.attributeValue("id");//获取id属性值
String clazz = element.attributeValue("class"); //获取class属性值
BeanDefinition beanDefine = new BeanDefinition(id, clazz);
beanDefinitions.add(beanDefine);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Object getBean(String fileName) {
return this.sigletons.get(fileName);
}
}
--------------------------------------------------------------------------------
BeanDefinition :
package commons.tz;
public class BeanDefinition {
private String id;
private String className;
public BeanDefinition(String id, String className) {
this.id = id;
this.className = className;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
--------------------------------------------------------------------------------
Spring02Test :
package junit.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import commons.tz.TzClassPathXmlApplicationContext;
import cn.service.PersonService;
public class Spring02Test {
@Test
public void instanceSping() {
// ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
TzClassPathXmlApplicationContext ctx = new TzClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");
personService.save();
}
}
本来不准备把这部分贴上来的,但是最近项目中出现了离线word协同办公的需求,于是通过office2007特有的文件格式特征,解析xml文件然后对数据进行存储和分析。以上可以进行借鉴,等项目中期编码成熟了,再搞个专题,这里就权且当一个Mark^_^!
浙公网安备 33010602011771号