Spring简单的小例子SpringDemo,用于初略理解什么是Spring以及JavaBean的一些概念
一、开发前的准备
两个开发包spring-framework-3.1.1.RELEASE-with-docs.zip和commons-logging-1.2-bin.zip,将它们解压,然后把Spring开发包下dist目录的所有包和commons-logging包下的commons-logging-1.1.1.jar复制到名为Spring3.1.1的文件夹下。那么Spring开发所需要的包就组织好了。
二、建立项目,导入包
在项目节点上右键,Build Path/ADD Libraries/USER Libraries/new/输入包名Spring3.1.1/OK/ADD JARS/选中准备好的包/OK。此外引入JUnit4方便程序测试。

三、添加文件

person接口Person.Java:
package com.bean;
public interface Person{
public void Speak();
}
AmericanImpl.java:
package com.bean;
public class AmericanImpl implements Person{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void Speak() {
// TODO Auto-generated method stub
System.out.println("I'm American,My name is "+this.name+",I'm "+this.age+" years old!");
}
}
ChineseImpl.java:
package com.bean;
public class ChineseImpl implements Person{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void Speak() {
// TODO Auto-generated method stub
System.out.println("I'm Chinese,My name is "+this.name+",I'm "+this.age+" years old!");
}
}
配置文件applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="chinese" class="com.bean.ChineseImpl">
<property name="name">
<value>小明</value>
</property>
<property name="age">
<value>10</value>
</property>
</bean>
<bean id="american" class="com.bean.AmericanImpl">
<property name="name">
<value>Tom</value>
</property>
<property name="age">
<value>15</value>
</property>
</bean>
</beans>
用于测试的Test.java:
package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bean.Person;
public class Test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=(Person)context.getBean("chinese");
person.Speak();
person=(Person)context.getBean("american");
person.Speak();
}
}
四、测试结果
在Test.Java上右击Run As|Java Application,输出结果如下:

Spring简单的小例子SpringDemo,用于初略理解什么是Spring以及JavaBean的一些概念的更多相关文章
- 基于vue-cli、elementUI的Vue超简单入门小例子
- 这个例子还是比较简单的,独立完成后,能大概知道vue是干嘛的,可以写个todoList的小例子. - 开始写例子之前,先对环境的部署做点简单的介绍,其实和Vue官方的差不多. #如若没有安装过vu ...
- spring的一个小例子(二)--解析前面的小例子
接上篇:http://www.cnblogs.com/xuejupo/p/5236448.html 首先应该明白,一个web项目,web.xml是入口. 然后下面来分析上篇博客中出现的web.xml: ...
- java入门---简介&简单输出小例子&开发前准备
Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式推出.J ...
- 一个简单的小例子让你明白c#中的委托-终于懂了!
模拟主持人发布一个问题,由多个嘉宾来回答这个问题. 分析:从需求中抽出Host (主持人) 类和Guests (嘉宾) 类. 作为问题的发布者,Host不知道问题如何解答.因此它只能发布这个事件,将事 ...
- Asp.net MVC4之 一个简单的小例子
练习: 新建一个mvc项目 要求: 有3个视图 Login Index Details 目的:感受一下MVC与传统WebForm的差异性 WebForm的请求模型 MVC请求模型 传统WebForm ...
- js 继承的简单易懂小例子
js 继承 今天主要说原型链继承.构造继承.组合继承三种常用继承方式,分享一下我的理解. 原型链继承例子1 //原型继承function A(name){ this.name = name;}func ...
- Spring简单的REST例子
控制器处理 HTTP 的4个主要方法 GET,POST,PUT,DELETE 使用@PathVariable注解获取URL中的参数 import javax.inject.Inject; import ...
- appium基础之简单的小例子
appium环境搭建了,当然也要开始用起来了,记录一下学习的过程 遇到问题 1.The permission to start '.ui.home.view.HomeActivity' activit ...
- maven+springmvc+cxf 实现简单webservice小例子
1.首先你需要创建一个maven项目[当然是web项目] 2.pom.xml添加以下 <properties> <cxf.version>2.2.3</cxf.versi ...
随机推荐
- DOM---documentFragment
代码暂时寄存 创建:document.createDocumentFragment() 案例: var a=document.createDocumentFragment(); var b=docum ...
- Java导入证书失败Keystore was tampered with, or password was incorrect
keytool 错误: java.io.IOException: Keystore was tampered with, or password was incorrect 在进行证书相关操作, ...
- JQuery里属性赋值,取值prop()和attr()方法?
1.赋值的时候 如果是<input type="checkbox" checked>这样的只有属性名就能生效的属性 推荐prop,即:$('input').prop(' ...
- mysql_config not found
在python中安装MySQL_python.不想通过下载源码编译,而是想用 easy_install MySQL_python 来安装.结果一直报错: mysql_config not found ...
- Vxworks、QNX、Xenomai、Intime、Sylixos、Ucos等实时操作系统的性能特点
Vxworks.QNX.Xenomai.Intime.Sylixos.Ucos等实时操作系统的性能特点 VxWorks操作系统 VxWorks 操作系统是美国WindRiver公司于1983年设计开发 ...
- 获取MP3和M4A音乐文件的歌曲信息以及专辑图片--备用
NSBundle* bundle = [NSBundle mainBundle]; NSString* path = [bundle bundlePath]; NSURL * file ...
- android adb 常用指令
转自:http://www.cnblogs.com/playing/archive/2010/09/19/1830799.html Android 调试桥(adb)是多种用途的工具,该工具可以帮助你你 ...
- 转:DDR3详解(以Micron MT41J128M8 1Gb DDR3 SDRAM为例)之一
转载来自:http://blog.csdn.net/shanghaiqianlun/article/details/6976804 作者:shanghaiqianlun的专栏 1.结构框图: 2.管脚 ...
- 在网页中获取 facebook page 的内容
参考 : http://www.ibm.com/developerworks/cn/opensource/os-cn-facebookapi/ 1.首先你要有 facebook page, 内容要公开 ...
- qt编写一个只能运行单个实例的程序,不用Windows API
QtSingleApplicationhttp://code.qt.io/cgit/qt-solutions/qt-solutions.git/tree/qtsingleapplication 已打开 ...