一、开发前的准备

两个开发包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的一些概念的更多相关文章

  1. 基于vue-cli、elementUI的Vue超简单入门小例子

    - 这个例子还是比较简单的,独立完成后,能大概知道vue是干嘛的,可以写个todoList的小例子. - 开始写例子之前,先对环境的部署做点简单的介绍,其实和Vue官方的差不多. #如若没有安装过vu ...

  2. spring的一个小例子(二)--解析前面的小例子

    接上篇:http://www.cnblogs.com/xuejupo/p/5236448.html 首先应该明白,一个web项目,web.xml是入口. 然后下面来分析上篇博客中出现的web.xml: ...

  3. java入门---简介&简单输出小例子&开发前准备

        Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式推出.J ...

  4. 一个简单的小例子让你明白c#中的委托-终于懂了!

    模拟主持人发布一个问题,由多个嘉宾来回答这个问题. 分析:从需求中抽出Host (主持人) 类和Guests (嘉宾) 类. 作为问题的发布者,Host不知道问题如何解答.因此它只能发布这个事件,将事 ...

  5. Asp.net MVC4之 一个简单的小例子

    练习: 新建一个mvc项目 要求: 有3个视图  Login Index Details 目的:感受一下MVC与传统WebForm的差异性 WebForm的请求模型 MVC请求模型 传统WebForm ...

  6. js 继承的简单易懂小例子

    js 继承 今天主要说原型链继承.构造继承.组合继承三种常用继承方式,分享一下我的理解. 原型链继承例子1 //原型继承function A(name){ this.name = name;}func ...

  7. Spring简单的REST例子

    控制器处理 HTTP 的4个主要方法 GET,POST,PUT,DELETE 使用@PathVariable注解获取URL中的参数 import javax.inject.Inject; import ...

  8. appium基础之简单的小例子

    appium环境搭建了,当然也要开始用起来了,记录一下学习的过程 遇到问题 1.The permission to start '.ui.home.view.HomeActivity' activit ...

  9. maven+springmvc+cxf 实现简单webservice小例子

    1.首先你需要创建一个maven项目[当然是web项目] 2.pom.xml添加以下 <properties> <cxf.version>2.2.3</cxf.versi ...

随机推荐

  1. iOS9中请求出现App Transport Security has blocked a cleartext HTTP (http://)

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

  2. mysql学习(用户权限管理)

    1. 添加数据库用户 create user 'username'@'host' identified by 'password'; 提示: 如果想让该用户可以从其他主机登陆,host可以设置为'%' ...

  3. 武汉科技大学ACM :1007: 华科版C语言程序设计教程(第二版)习题7.10

    Problem Description 输入n(n<100)个字符串,每个字符串长度不超过1000,将他们按字典顺序输出. Input 多组测试样例. 每组第一行有一个整数n表示有n个字符串. ...

  4. Ruby设计模式透析之 —— 适配器(Adapter)

    转载请注明出处:http://blog.csdn.net/sinyu890807/article/details/9400153 此为Java设计模式透析的拷贝版,专门为Ruby爱好者提供的,不熟悉R ...

  5. STL删除vector或list的方法及注意的问题

    删除vector中的元素 1.删除指定的所有对象 STL中remove()只是将待删除元素之后的元素移动到vector的前端,而不是删除.若要真正移除,需要搭配使用erase().例子: vector ...

  6. 关于C++中的虚拟继承的一些总结

    1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念.虚拟基类是为解决多重继承而出现的.如:类D继承自类B1.B2,而类B1.B2都继承自类A,因此在类D中两次出现类A中的变量和函数.为了节省内存 ...

  7. FlowPlayer 参数说明

    <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> & ...

  8. addChildViewController 用法

    // // SCMyOrderViewController.m // SmartCommunity // // Created by chenhuan on 15/9/7. // Copyright ...

  9. SQL Server 触发器(转)

    触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...

  10. bootstrap--- 两种bootstrap multiselect组件大比拼

    http://www.cnblogs.com/landeanfen/p/5013452.html 1.第一种可以兼容IE,第二种不能兼容