---------------siwuxie095

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

创建一个基于
Spring IoC
的小程序的步骤:

 
 

  • 建立
    Spring
    工程
  • 编写
    Java
    文件
  • 编写配置文件
  • 运行示例工程

     
     

     
     

     
     

    示例:

     
     

    一个人,在中国时用中文问候大家;在外国时,用英语问候大家

     
     

    人的具体位置,由
    Spring
    的配置环境来决定的:

    ·
    当配置为中国时,则问候:"大家好"

    ·
    当配置为外国时,则问候:"Hello everybody"

     
     

     
     

     
     

     
     

     
     

    工程名:TestHelloMessage

    包名:com.siwuxie095.spring

    类名:HelloMessage.java、HelloChina.java、HelloWorld.java、

    Person.java、Main.java(主类)

     
     

     
     

    打开资源管理器,在工程 TestHelloMessage 文件夹下,创建一个

    文件夹:lib,在其中放入三个必需的 jar 包:

    (1)spring-core-4.3.7.RELEASE.jar

    (2)spring-beans-4.3.7.RELEASE.jar

    (3)commons-logging-1.2.jar

     
     

     
     

    选择三个
    jar 包,右键->Build Path->Add to Build Path

     
     

    工程结构目录如下:

     
     

     
     

     
     

     
     

     
     

    HelloMessage.java:

     
     

package com.siwuxie095.spring;

 
 

//接口 HelloMessage,用于定义输出问候信息

public interface HelloMessage {

/**

* 方法的简写,因为接口中只允许存在抽象方法和全局常量

* 所以 public 和 abstract 可以省略掉
或只省略 abstract

*/

public String sayHello();

 
 

}

 
 

 
 

 
 

HelloChina.java:

 
 

package com.siwuxie095.spring;

 
 

// HelloChina 是接口的实现类,用于输出
大家好

public class HelloChina implements HelloMessage {

 
 

@Override

public String sayHello() {

return
"大家好";

}

 
 

}

 
 

 
 

 
 

HelloWorld.java:

 
 

package com.siwuxie095.spring;

 
 

// HelloWorld 是接口的实现类,用于输出 Hello everybody

public class HelloWorld implements HelloMessage {

 
 

@Override

public String sayHello() {

return
"Hello everybody";

}

 
 

}

 
 

 
 

 
 

Person.java:

 
 

package com.siwuxie095.spring;

 
 

//Person类,用于调用 HelloMessage 接口,输出问候信息

public class Person {

 

//将HelloMessage作为一个属性,用于向大家输出问候信息

private HelloMessage helloMessage;

 
 

public HelloMessage getHelloMessage() {

return helloMessage;

}

 
 

public
void setHelloMessage(HelloMessage helloMessage) {

this.helloMessage = helloMessage;

}

 

/**

* 用于调用HelloMessage接口向用户输出问候信息,

* 具体的问候信息,由Spring的配置文件来分配和决定:

* 1.当配置文件中分配给person的是HelloChina的实体时,则输出"大家好"的信息

* 2.当配置文件中分配给person的是HelloWorld的实体时,则输出"Hello everybody"的信息

*/

public String sayHello() {

return this.helloMessage.sayHello();

}

 

 
 

}

 
 

 
 

 
 

Main.java(主类):

 
 

package com.siwuxie095.spring;

 
 

import org.springframework.beans.factory.support.DefaultListableBeanFactory;

import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

import org.springframework.core.io.FileSystemResource;

import org.springframework.core.io.Resource;

 
 

public class Main {

 
 

/**

* 代码段(1)和(2)等效

* 因为 Spring 3.1 之后已经废弃 XmlBeanFactory

* 所以这里注释掉(2),使用(1)

* 但(2)依然可用,导入相关包和类即可

*/

public static
void main(String[] args) {

 

//(1)

//使用 FileSystemResource 来读取配置文件

Resource resource=new FileSystemResource("helloMessage.xml");

DefaultListableBeanFactory factory=new DefaultListableBeanFactory();

XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory);

reader.loadBeanDefinitions(resource);

//从IOC容器中获取Person类的实例

//Person person=(Person)b.getBean("Person");

Person person= (Person) factory.getBean("person");

//使用person类示例来输出问候信息

String str=person.sayHello();

System.out.println("The Person is currently saying:"+str);

 

 

// //(2)

// //使用 FileSystemResource() 读取配置文件 helloMessage.xml

// Resource resource=new FileSystemResource("helloMessage.xml");

// //使用 XmlBeanFactory() 加载配置文件,启动 IoC 容器

// BeanFactory factory=new XmlBeanFactory(resource);

// //从 IoC 容器中获取 Person 类的实例

// Person person=(Person) factory.getBean("person");

// String str=person.sayHello();

// System.out.println("The person is currently saying:"+str);

 

}

 
 

}

 
 

 
 

 
 

创建配置文件
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">

 
 

 
 

<!-- id 和 class 是 bean 的属性,id 是唯一标识,class 是对应的类名(完全限定名) -->

<bean
id="helloChina"
class="com.siwuxie095.spring.HelloChina">

</bean>

<bean
id="helloWorld"
class="com.siwuxie095.spring.HelloWorld">

</bean>

 

<!-- 为 person 添加属性,该属性引用了bean:helloWorld -->

<!-- 此时,person 就和 helloWorld 建立了依赖关系 -->

<!-- 也可以引用 helloChina,只需要修改 ref 属性即可 -->

<!-- 它们之间的依赖关系,根据配置文件决定,具体配置谁,控制权由原来的对象本身转移到配置文件中 -->

<bean
id="person"
class="com.siwuxie095.spring.Person">

<property
name="helloMessage"
ref="helloWorld"></property>

</bean>

 
 

</beans>

 
 

 
 

此时,工程结构目录一览:

 
 

 
 

 
 

 
 

运行一览:

 
 

 
 

 
 

 
 

 
 

Spring Bean 和 JavaBean 的区别:

 
 

参考链接1参考链接2参考链接3参考链接4

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

【made by siwuxie095】

Spring示例工程的更多相关文章

  1. 使用方法拦截机制在不修改原逻辑基础上为 spring MVC 工程添加 Redis 缓存

    首先,相关文件:链接: https://pan.baidu.com/s/1H-D2M4RfXWnKzNLmsbqiQQ 密码: 5dzk 文件说明: redis-2.4.5-win32-win64.z ...

  2. Ubuntu下使用VS Code创建Spring Boot工程

    目的 我们将在Ubuntu桌面系统下,使用VS Code(Visual Studio Code)编辑器从零开始创建一个Spring Boot工程,并实现一个简单的RESTful风格接口.使用这套流程的 ...

  3. Spring Boot工程发布到Docker

    先聊聊闲话 搞过企业级的application运维的同仁肯定深有感触,每个application的功能交叉错杂,数据交换就让人焦头烂额(当然这和顶层业务设计有关系), 几十个application发布 ...

  4. 【AT91SAM3S】英倍特串口示例工程05-UART中,串口是怎样初始化的

    在这个示例工程的main.c文件中,进入main之后,没有发现串口功能的任何配置.直接使用了printf这个东西进行输出.将软件下载到开发板上之后,在电脑端使用串口软件,可以看板子有数据发来.说明这个 ...

  5. Springboot(一):使用Intellij中的Spring Initializr来快速构建Spring Boot工程

    使用Intellij中的Spring Initializr来快速构建Spring Boot工程 New---Project 可以看到图所示的创建功能窗口.其中Initial Service Url指向 ...

  6. spring cloud 工程构建

    https://blog.csdn.net/zhou199252/article/details/80745151 https://blog.csdn.net/forezp/article/detai ...

  7. SpringCloud核心教程 | 第一篇: 使用Intellij中的Spring Initializr来快速构建Spring Cloud工程

    spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...

  8. SpringCloud核心教程 | 第二篇: 使用Intellij中的maven来快速构建Spring Cloud工程

    spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...

  9. activiti学习2:示例工程activiti-explorer.war的使用

    目录 activiti学习2:示例工程activiti-explorer.war的使用 一.搭建开发环境 二.运行示例工程 三.示例工程功能演示 1. 创建流程图 2. 部署流程图 3. 启动流程 4 ...

随机推荐

  1. 基于Linux Shell的开机启动服务

    CentOS重启后,很多服务需要手动启动,很是麻烦,今天把需要开机启动或关闭的服务整理了一下,放入Linux Shell中,再将该Shell加入/etc/rc.local中,即可实现存储的自动挂载.S ...

  2. 《DevExpress》记录之TreeList

    如这两幅图所示:如果要显示左边的竖线,需要设置 感谢 DoomGuards本节Dome下载地址:http://pan.baidu.com/s/1wBOJk 密码:vz4d

  3. jquery中篇

    一.attr 返回属性值 返回被选元素的属性值. 语法 $(selector).attr(attribute) 参数 描述 attribute 规定要获取其值的属性. 属性 • 属性 o attr(n ...

  4. hbase shell-general(常规指令)

    hbase shell常规指令解释篇 1. status (显示集群状态,master,server情况,显示内容的详略程度可选) hbase(main)::> help 'status' Sh ...

  5. 剑指offer——二叉树的深度与平衡二叉树的判断

    通过后续遍历,可以减少重复访问 #include <iostream> #include <string> using namespace std; struct Binary ...

  6. poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】

    题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...

  7. Wildfly在Linux下以Service的方式启动 配置步骤

    1.前提介绍 在目前项目中使用Wildfly9在linux下部署项目,经常会通过远程的SSH来启动关闭服务.但是通过SHH启动 standalone.sh 的服务,如果关闭窗口服务就会被停掉.所以就想 ...

  8. 命令行 -- 命令"%cd%"

    1. @echo off echo 当前盘符:%~d0 echo 当前盘符和路径:%~dp0 echo 当前批处理全路径:%~f0 echo 当前盘符和路径的短文件名格式:%~sdp0 echo 当前 ...

  9. C++指向函数的指针

    直接上代码: #include<iostream> #include<string> #include<vector> using namespace std; t ...

  10. 解决微信浏览器video全屏的问题

    解决微信浏览器video全屏的问题 在微信浏览器里面使用video标签,会自动变成全屏,改成下面就好了,起码可以在video标签之上加入其他元素. <video id="videoID ...