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的开发环境的更多相关文章

  1. Spring MVC 项目搭建 -3- 快速 添加 spring security

    Spring MVC 项目搭建 -3- 快速 添加 spring security 1.添加 spring-sample-security.xml <!-- 简单的安全检验实现 --> & ...

  2. 【前端福利】用grunt搭建自动化的web前端开发环境-完整教程

    jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前端开发人员,如果你现在还不知道grunt或者听说过 ...

  3. 转:【前端福利】用grunt搭建自动化的web前端开发环境-完整教程

    原文地址:http://blog.csdn.net/wangfupeng1988/article/details/46418203 jQuery在使用grunt,bootstrap在使用grunt,百 ...

  4. Android开发学习总结(一)——搭建最新版本的Android开发环境

    Android开发学习总结(一)——搭建最新版本的Android开发环境(转) 最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是 ...

  5. 用grunt搭建自动化的web前端开发环境实战教程(详细步骤)

    用grunt搭建自动化的web前端开发环境实战教程(详细步骤) jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用!前端自动化, ...

  6. 用grunt搭建自动化的web前端开发环境-完整教程

    原稿:http://www.cnblogs.com/wangfupeng1988/p/4561993.html#!comments jQuery在使用grunt,bootstrap在使用grunt,百 ...

  7. 图文详解如何搭建Windows的Android C++开发环境

    原地址:http://www.apkbus.com/android-18595-1-1.html //================================================= ...

  8. EditPlus+MinGW搭建简易的C/C++开发环境

    EditPlus+MinGW搭建简易的C/C++开发环境 有时候想用C编点小程序,但是每次都要启动那难用又难看的VC实在是不情愿,而且老是会生成很多没用的中间文件,很讨厌,后来看到网上有很多人用Edi ...

  9. 用grunt搭建自动化的web前端开发环境

    用grunt搭建自动化的web前端开发环境 jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前端开发 ...

随机推荐

  1. HeadFisrt 设计模式03 装饰者

    类应该对扩展开放, 对修改关闭. 所谓装饰者模式, 是指用其他的类来装饰某个类, 装饰者说白了就是使用 has-a 来代替 is-a 隐喻 咖啡店, 有很多种咖啡, 咖啡里还要增加一些 milk, 面 ...

  2. 【BZOJ】1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏(刷水严重)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1666 这种我就不说了.. #include <cstdio> #include < ...

  3. oh my zsh 切换 bash

    zsh切换bash bash切换zsh 切换bash chsh -s /bin/bash 切换zsh chsh -s /bin/zsh

  4. Laravel5.1 关联模型之后操作

    之前写过关于模型关联的笔记,但是模型关联好后的一些使用没有介绍,今天补上 1 写入关联模型 1.1 使用Save方法(一对多) 我们准备了两个模型:Post和Comment. 它们的关系是一对多关系. ...

  5. windows 2003 发布遇到问题---分析器错误消息: 未能加载类型“YWPT.MvcApplication”。

    问题如下: “/”应用程序中的服务器错误. ------------------------------------------------------------------------------ ...

  6. Linq------错误: Unable to determine the principal end of an association between the types

    [Table("bma_stores")] public class Store { //加上即可 [Required] public virtual Product Produc ...

  7. videoview 播放视频

    -videoVIew 继承SurfaceView 使用android的VideoView来播放一个视频,步骤是: 1,在xml中创建一个videoView, 2,在java中导入,然后创建一个Medi ...

  8. iOS各种问题处理

    本文转载至:http://www.cnblogs.com/ygm900/category/436923.html 推荐初学者前去学习.     mac 拷贝文件时报错 8060 解决方案 摘要: 解决 ...

  9. globalToLocal和localToGlobal

    官方API: groupOut全局坐标(50,50) gourpIn全局坐标(100,100),并嵌套在groupOut里 btn全局坐标(150,150),并嵌套在groupIn里 获取组件全局坐标 ...

  10. SpringBoot--属性加载顺序

    属性加载顺序: 1.在命令行中传入的参数: 2.SPRING_APPLICATION_JSON中的属性:SPRING_APPLICATION_JSON是以JSON格式配置在系统环境变量中内容: 3.j ...