zoukankan      html  css  js  c++  java
  • Spring示例工程

    ---------------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 添加属性,该属性引用了beanhelloWorld -->

    <!-- 此时,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】

  • 相关阅读:
    【动画技巧】在Flash中自定义鼠标外观
    【动画技巧】GIF动画转SWF小技巧
    SQL Server 2008空间数据应用系列十一:提取MapInfo地图数据中的空间数据解决方案
    SQL Server 2008空间数据应用系列十:使用存储过程生成GeoRSS聚合空间信息
    jQuery的模板与数据绑定插件
    ASP.NET MVC 入门5、View与ViewData
    一个jQuery写的虚拟键盘
    ASP.NET MVC 入门4、Controller与Action
    使用XML文件来动态配置ASP.NET MVC的Route规则
    ASP.NET MVC 入门9、Action Filter 与 内置的Filter实现(介绍)
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/6735467.html
Copyright © 2011-2022 走看看