Spring第二弹—–搭建与测试Spring的开发环境
PS:Spring既可以使用在javaSE中,也可以使用在javaWeb中。
使用Spring需要的jar
下载spring(我下载的是2.5.6版本),然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下
dist\spring.jar
commons-logging.jar
spring的配置文件模版
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?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>
|
该配置模版可以从spring的参考手册或spring的例子中得到。配置文件的取名可以任意,文件可以存放在任何目录下,但考虑到通用性,一般放在类路径下。
编写spring配置文件时,不能出现帮助信息
由于spring的schema文件位于网络上,如果机器不能连接到网络,那么在编写配置信息时候就无法出现提示信息,解决方法有两种:
1。让机器上网,eclipse会自动从网络上下载schema文件并缓存在硬盘上。
2。手动添加schema文件,方法如下:
windwos->preferences->myeclipse->files and editors->xml->xmlcatalog
点”add”,在出现的窗口中的Key Type中选择URI,在location中选”File system”,然后在spring解压目录的dist/resources目录中选择spring-beans-2.5.xsd,回到设置窗口的时候不要急着关闭窗口,应把窗口中的Key Type改为Schema location,Key改为http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
实例化spring容器
实例化Spring容器常用的两种方式:
方法一:(尽量用第一种,跨平台)
在类路径下寻找配置文件来实例化容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{“beans.xml”});
方法二:在文件系统路径下寻找配置文件来实例化容器
ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{“d:\\beans.xml“});
Spring的配置文件可以指定多个,可以通过String数组传入。
从spring容器中得到bean
当spring容器启动后,因为spring容器可以管理bean对象的创建,销毁等生命周期,所以我们只需从容器直接获取Bean对象就行,而不用编写一句代码来创建bean对象。从容器获取bean对象的代码如下:
ApplicationContext ctx = new ClassPathXmlApplicationContext(“beans.xml”);
PersonService ps = (PersonService) ctx.getBean(“personService”);
规范:
Id与name都可以为bean取名,名称要唯一,尽量第一个字母小写。
PS:id本身就属于xml一个属性,这个属性受xml解析器进行验证,id值不能包含特殊字符,如\。
范例:第一个Spring程序
Java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package zmcjk;
public interface PersonService {
void save();
}
package zmc;
import zmcjk.PersonService;
public class PersonServicebean implements PersonService {
public void save(){
System.out.println("我是save()方法");
}
}
配置文件:
<?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="zmc.PersonServicebean"></bean>
</beans>
测试类:
package Test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import zmcjk.PersonService;
public class SpringD {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void T() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService ps = (PersonService) ctx.getBean("personService");
ps.save();
}
}
|
Spring第二弹—–搭建与测试Spring的开发环境的更多相关文章
- Spring MVC 项目搭建 -3- 快速 添加 spring security
Spring MVC 项目搭建 -3- 快速 添加 spring security 1.添加 spring-sample-security.xml <!-- 简单的安全检验实现 --> & ...
- 【前端福利】用grunt搭建自动化的web前端开发环境-完整教程
jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前端开发人员,如果你现在还不知道grunt或者听说过 ...
- 转:【前端福利】用grunt搭建自动化的web前端开发环境-完整教程
原文地址:http://blog.csdn.net/wangfupeng1988/article/details/46418203 jQuery在使用grunt,bootstrap在使用grunt,百 ...
- Android开发学习总结(一)——搭建最新版本的Android开发环境
Android开发学习总结(一)——搭建最新版本的Android开发环境(转) 最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是 ...
- 用grunt搭建自动化的web前端开发环境实战教程(详细步骤)
用grunt搭建自动化的web前端开发环境实战教程(详细步骤) jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用!前端自动化, ...
- 用grunt搭建自动化的web前端开发环境-完整教程
原稿:http://www.cnblogs.com/wangfupeng1988/p/4561993.html#!comments jQuery在使用grunt,bootstrap在使用grunt,百 ...
- 图文详解如何搭建Windows的Android C++开发环境
原地址:http://www.apkbus.com/android-18595-1-1.html //================================================= ...
- EditPlus+MinGW搭建简易的C/C++开发环境
EditPlus+MinGW搭建简易的C/C++开发环境 有时候想用C编点小程序,但是每次都要启动那难用又难看的VC实在是不情愿,而且老是会生成很多没用的中间文件,很讨厌,后来看到网上有很多人用Edi ...
- 用grunt搭建自动化的web前端开发环境
用grunt搭建自动化的web前端开发环境 jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前端开发 ...
随机推荐
- 情商 EQ & 儿童情商
EQ 包括哪些内容 1. 认知自身情绪的能力(正确客观的评价自己)2. 管理自己情绪的能力(控制冲动) 3. 自我激励能力(学会抗挫折) 4. 认识他人情绪的能力(学会移情) 5. 人际关系处理能力 ...
- shell向python传参数
想要在shell中调用python脚本时实现: python pyServer.py argu1 argu2 argu3 利用 sys.argv 即可读取到 相应参数: # coding=utf-8 ...
- Could not contact Selenium Server; have you started it on 'localhost:4444'
今天学习selenium RC例子的时候遇到一个问题:java.lang.RuntimeException: Could not contact Selenium Server; have you s ...
- Differential Geometry之第九章常平均曲率曲面
第九章.常平均曲率曲面 1.Hopf微分与Hopf定理 等温坐标系(isothermal coordinate system)曲面上的一种特殊坐标系.若曲面的第一基本形式I在坐标系(u,v)下可以写成 ...
- JavaBeans 官方文档学习
提示,重点:JavaBeans的Property和 Events:PropertyEditor极其注册和查找机制. 从目前来看,JavaBeans 更像是源自GUI的需求. 使用NetBeans新建一 ...
- 第二百四十七节,Bootstrap按钮和折叠插件
Bootstrap按钮和折叠插件 学习要点: 1.按钮 2.折叠 本节课我们主要学习一下 Bootstrap 中的按钮和折叠插件. 一.按钮 可以通过按钮插件创建不同状态的按钮,也就是点击后为选中状态 ...
- Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试。
Lifecycle for overriding binding, validation, etc,易于同其它View框架(Tiles等)无缝集成,采用IOC便于测试. 它是一个典型的教科书式的mvc ...
- HttpServlet中,用来处理POST请求的方法是(选择1项)
HttpServlet中,用来处理POST请求的方法是(选择1项) A. doHead B. doGet C. doPost D. doPut 解答:C
- [NOIP 2014复习]第二章:搜索
一.深度优先搜索(DFS) 1.Wikioi 1066引水入城 题目描写叙述 Description 在一个遥远的国度,一側是风景秀美的湖泊,还有一側则是漫无边际的沙漠.该国的行政 区划十分特殊,刚好 ...
- JavaScript第三天 boolean和json
布尔值 true:非零数字.非空字符串.非空对象 false:数字零.空字符串.null空对象.undefined json JSON(JavaScript Object Notation) 是一种 ...