Spring学习(六)实例演示Spring的使用
来源:互联网 发布:当当网和淘宝网哪个好 编辑:程序博客网 时间:2024/06/05 11:46
本次我们要编写一个输出问候信息的小例子:
当一个人在中国的时候,使用中国话向大家问好。当他去国外旅游时就需要用英语向大家问好。
首先我们得了解创建一个基于Spring IOC的小程序的具体步骤如下:
1) 建立Spring工程
2) 编写Java文件
3) 编写配置文件
4) 运行示例工程
新建一个Spring工程可以参考我之前的 Spring学习(一)在IntelliJ IDEA上使用Maven创建Spring项目
接下来我们来分析下这个例子当中的java类之间的关系:
这个项目可以简单的理解为:
一个人,在中国时用中国话问候大家;当去国外旅游的时候,用英语问候大家
人的具体位置,是由Spring的配置环境来决定的:
当配置为中国时,则问候“大家好!”
当配置为国外时,则问候“Hello everybody!”
在这里,我们在工程中逐个建立Java文件:
IHelloMessage:一个接口,用于定义输出问候信息
public interface IHelloMessage { public String sayHello();}HelloWorld:接口的实现类,向用户输出“Hello everybody”信息
public class HelloWorld implements IHelloMessage{ public String sayHello() { return "Hello Everybody"; }}HelloChina:接口的实现类,向用户输出“大家好!”信息
public class HelloChina implements IHelloMessage{ public String sayHello() { return "大家好!"; }}Person:一个人物类,调用IHelloMessage接口,向用户输出问候信息
public class Person { //因为Person类要引用IHelloMessage接口,所以将IHelloMessage作为类的一个属性 private IHelloMessage iHelloMessage; /** * 自动生成get方法 * @return */ public IHelloMessage getiHelloMessage() { return iHelloMessage; } /** * 自动生成set方法 * @param iHelloMessage */ public void setiHelloMessage(IHelloMessage iHelloMessage) { this.iHelloMessage = iHelloMessage; } /** * person类使用sayHello方法来调用借口的sayHello方法 * @return */ public String sayHello(){ return this.iHelloMessage.sayHello(); }}
Main:程序的入口类,用于加载配置文件以及启动IOC容器,调用人物类,向用户输出问候信息
注:Spring3.1之后已经废弃XmlBeanFactory,替换方式如下所示
public class Main { public static void main(String[] args){ //使用FileSystemResource来读取配置文件 Resource r=new FileSystemResource("helloMessage.xml"); //使用XmlBeanFactory加载配置信息,启动IOC容器 //Spring3.1之后已经废弃XmlBeanFactory //BeanFactory b=new XmlBeanFactory(r); DefaultListableBeanFactory factory=new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory); reader.loadBeanDefinitions(r); //从IOC容器中获取Person类的实例 //Person person=(Person)b.getBean("Person"); Person person= (Person) factory.getBean("Person"); //使用person类示例来输出问候信息 String s=person.sayHello(); System.out.println("The Person is currently saying "+s); }}接下来,为示例代码建立配置文件helloMessage.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="HelloChina" class="cn.lovepi.chapter01.HelloChina"> </bean> <bean id="HelloWorld" class="cn.lovepi.chapter01.HelloWorld"> </bean> <bean id="Person" class="cn.lovepi.chapter01.Person"> <!--bean中属性配置,属性即为类中所声明的接口变量。给其添加了一个引用--> <property name="iHelloMessage" ref="HelloWorld"> </property> </bean></beans>
在这里我们为每一个javaBean都声明了他的名称和具体类。在Person类中,因为有一个IHelloMessage接口的对象声明,所以得给Person类的bean中声明一个属性Property,name为声明的接口对象名称,ref为接口的具体实现类,在本示例中HelloWorld和HelloChina都属于接口的实现类,则可以根据更改ref的值来实现具体使用那一个实现类。
编译并运行示例工程,在控制台查看输出信息,验证程序运行是否正常:
通过配置文件,来控制人的输出信息
当人在国内时,是否输出了“大家好!”的信息
当人在国外时,是否输出了“Hello everybody!”的信息
总结:
通过这个简单的小示例,使我认识到了Spring框架的冰山一角,正所谓“管中窥豹,可见一斑”,由此也对于Spring的开发框架有了一个大致的了解。认识了其利用配置文件来实现IoC的具体实现,充分体会到了利用配置文件控制系统的快速与便捷性。
1 0
- Spring学习(六)实例演示Spring的使用
- Spring学习之路 第一个Spring演示实例
- Spring学习笔记(六):Rest的使用
- Spring学习(六)spring整合注解形式的hibernate
- spring学习(六)spring中的BeanPostProcessor
- spring的继承使用<六>
- Spring学习笔记(六)
- Spring学习笔记(六)
- spring学习笔记(六)
- Spring学习笔记(六)
- Spring 学习笔记(六)
- 关于Spring项目配置多个数据源的实例演示
- Spring学习之旅(六) 表单校验以及Spring标签库的使用
- 分布式缓存技术redis学习系列(六)—— 深入理解Spring Redis的使用
- 学习Spring笔记六
- Spring Boot学习(六)
- Spring的AOP使用实例
- Spring学习笔记 (六) 在Spring中使用AOP例子
- 二叉搜索树
- Android-基本UI控件
- 151_app社会化分享
- 安卓消息机制
- 《Spring设计思想》AOP设计基本原理
- Spring学习(六)实例演示Spring的使用
- 介绍Linux操作系统下的USB硬件设备驱动
- 小项目:简单注释转换(C风格转到C++风格)
- 记录的开始
- 152_gradle的构建
- 五、友录-拨号页面实现
- ion-scroll 滑动自动返回解决方法记录
- Rxjava Subject分析
- 如何设置一个严格30分钟过期的Session