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前端开发 ...
随机推荐
- Libgdx多线程与渲染线程
http://www.leestorm.com/post/115.html ——————————————————————————————————————————————————————————‘ 大部 ...
- 你不知道的JavaScript学习笔记1——作用域
处理程序三要素: 引擎:编译与执行过程. 编译器:语法分析与代码生成等. 作用域:收集并维护由所有声明的标识符(变量)组成的一系列查询,并实施一套非常严格的规则,确定当前执行的代码对这些标识符的访问权 ...
- javax.Servlet的包中,属于类的是。(选择1项)
javax.Servlet的包中,属于类的是.(选择1项) A.Servlet B.GenericServlet C.ServletRequest D.ServletContext 解答:B Serv ...
- 工业级别sd卡存贮slc mlc tlc
slc mlc tlc SLC = Single-Level Cell ,即1bit/cell,速度快寿命长,价格超贵(约MLC 3倍以上的价格),约10万次擦写寿命 MLC = Multi-Leve ...
- php -- php的事务处理
MYSQL的事务处理主要有两种方法. 1.用begin,rollback,commit来实现 begin 开始一个事务 rollback 事务回滚 commit 事务确认 2.直接用set来改变mys ...
- 【NOIP模拟题】Incr(dp)
太水的dp没啥好说的.. #include <cstdio> #include <cstring> #include <cmath> #include <st ...
- MySQL [Err]1449 : The user specified as a definer ('root'@'%') does not exist
权限问题:授权 给 root 所有sql 权限 mysql> grant all privileges on *.* to root@"%" identified by &q ...
- 使用 Estimator 构建卷积神经网络
来源于:https://tensorflow.google.cn/tutorials/estimators/cnn 强烈建议前往学习 tf.layers 模块提供一个可用于轻松构建神经网络的高级 AP ...
- 《算法竞赛入门经典》学习笔记 2.4.4 C++中的输入输出
2.4.3 64位整数输入输出long long除了cin,cout,也可以借助于printf和scanf语句,但对应的占位符缺是和平台与编译器相关的:在Linux中,gcc很同意的用%lld:在Wi ...
- URL地址传值型多条件搜索JS
function ResetSearchVal(objArray) { var strUrl = location.href; ; i < objArray.length; i++) { var ...