Spring基础:快速入门spring(2):理解Ioc

这里写图片描述

本文将使用简单的例子来说明spring的Ioc是如何实施的。

传统方式

首先我们要看一下不使用Ioc的时候得一个简单的例子。创建一个Teacher的类,此类有一个sayhello的函数,
在另外一个类里面创建实例并调用它。

创建Package

在src目录上右击,创建Java package(com.liumiao.demo.spring)

创建一个名为Teacher的Class

package com.liumiao.demo.spring;

public class Teacher {

    public String sayhello(){
        return "Hello, I am a teacher";
    }
}

创建用于演示的TestDemo的Class

package com.liumiao.demo.spring;

public class TestDemo {

  public static void main(String[] args){
    Teacher person = new Teacher();

    System.out.println(person.sayhello());
  }
}

执行TestDemo

结果如下

Hello, I am a teacher

传统方式的改进

一般的code使用最佳实践对代码进行优化,可能改成下面的样子

引入Person这个Interface

package com.liumiao.demo.spring;

public interface Person {
  public String sayhello();
}

改写Teacher类

package com.liumiao.demo.spring;

public class Teacher implements Person {
  @Override
  public String sayhello(){
    return "Hello, I am a teacher";
  }
}

修改TestDemo

package com.liumiao.demo.spring;

public class TestDemo {

  public static void main(String[] args){
    Person person = new Teacher();

    System.out.println(person.sayhello());
  }
}

执行TestDemo

结果如下

Hello, I am a teacher

结果没有变化。为什么要这样作呢,看起来只是绕圈实现而已,多做无用功的感觉。但其实把这个放大或者放到特定的上下文之中,其作用立即就会显现。举个简单的例子,我们想增加更多的子类的实现,比如实现一个Student的类,扩展或者替换,和其他设计模式进行结合都会非常方便,接下来来看看增加一个Student的实现需要做哪些事情。

增加一个Student的类

package com.liumiao.demo.spring;

public class Student implements Person {
  @Override
  public String sayhello(){
    return "Hello, I am a student.";
  }
}

修改TestDemo

package com.liumiao.demo.spring;

public class TestDemo {

  public static void main(String[] args){
    Person person = new Student();

    System.out.println(person.sayhello());
  }
}

执行TestDemo

结果如下

Hello, I am a student.

修改很少,OCP原则,开闭方便。source过于简单不便多说,可自行在项目中体会。虽然依然很简单有效,但是如果改变的话依然需要修改source,毕竟这里new Student这个操作被hardcoding了,接下来我们在这个基础之上来看看spring是如何做的。

Spring方式

创建spring的配置文件

我们在com.liumiao.demo.spring下面创建一个spring的配置文件spring-cfg.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">

</beans>

配置spring bean

接下来需要把一些bean设定到spring的配置文件中,我们先把Teacher这个bean设定进取,设定后的配置文件内容如下

<?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="thePerson" class="com.liumiao.demo.spring.Teacher">
  </bean>

</beans>

使用

配置好了之后,spring中使用方式一般按照如下四步

步骤内容参考
Step 1装载spring配置文件ClassPathXmlApplicationContext
Step 2从返回的context中检索相应的spring beancontext.getBean
Step 3执行spring bean
Step 4关闭contextcontext.close

改写TestDemo类

package com.liumiao.demo.spring;

import com.liumiao.demo.spring.Person;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDemo {
    public TestDemo() {
    }

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/liumiao/demo/spring/spring-cfg.xml");
        Person person = (Person)context.getBean("thePerson", Person.class);
        System.out.println(person.sayhello());
        context.close();
    }
}

执行结果

Nov 24, 2016 8:41:31 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@722c41f4: startup date [Fri Nov 24 20:41:30 CST 2016]; root of context hierarchy
Nov 24, 2016 8:41:31 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [com/liumiao/demo/spring/spring-cfg.xml]
Hello, I am a teacher
Nov 24, 2016 8:41:31 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
情報: Closing org.springframework.context.support.ClassPathXmlApplicationContext@722c41f4: startup date [Fri Nov 24 20:41:30 CST 2016]; root of context hierarchy

使用Student类

从使用Teacher类变成使用Student类,在改善过了的传统方式需要修改少量代码,而使用Spring的话,则只需要向如下这样修改spring设定文件即可

<?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="thePerson" class="com.liumiao.demo.spring.Student">
  </bean>

</beans>

执行结果

Nov 24, 2016 8:44:53 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
情報: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@722c41f4: startup date [Fri Nov 24 20:44:53 CST 2016]; root of context hierarchy
Nov 24, 2016 8:44:54 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
情報: Loading XML bean definitions from class path resource [com/liumiao/demo/spring/spring-cfg.xml]
Nov 24, 2016 8:44:54 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
情報: Closing org.springframework.context.support.ClassPathXmlApplicationContext@722c41f4: startup date [Fri Nov 24 20:44:53 CST 2016]; root of context hierarchy
Hello, I am a student.

这样就可以看到spring使用这种方式非常简单的通过修改设定文件来实现所需的功能的过程

备注:过程中如需用到commons-logging,下载地址为https://commons.apache.org/proper/commons-logging/download_logging.cgi
  • 31
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
JSONObject必包的Jar包及json生成的简单案例 所有commons包的网址: http://commons.apache.org/index.html 组装和解析JSONObject的Json字符串,共需要下面六个包: 1、json-lib 2、commons-beanutils 3、commons-collections 4、commons-lang 5、commons-logging 6、ezmorph 第零个包: json-lib-2.4-jdk15.jar http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/ 下载地址:http://nchc.dl.sourceforge.net/project/json-lib/json-lib/json-lib-2.4/json-lib-2.4-jdk15.jar 第一个包: commons-beanutils-1.9.2.jar http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi 下载地址:http://mirrors.cnnic.cn/apache//commons/beanutils/binaries/commons-beanutils-1.9.2-bin.zip 第二个包: (注:此包不可用,改用旧包) commons-collections4-4.0.jar http://commons.apache.org/proper/commons-collections/download_collections.cgi 下载地址:http://apache.dataguru.cn//commons/collections/binaries/commons-collections4-4.0-bin.zip (注:此包可用,低版本的包稳定性更高) commons-collections-3.2.1.jar http://commons.apache.org/proper/commons-collections/download_collections.cgi 下载地址:http://mirrors.hust.edu.cn/apache//commons/collections/binaries/commons-collections-3.2.1-bin.zip 第三个包: (注:此包不可用,会造成程序出错,改用旧包) commons-lang3-3.3.2.jar http://commons.apache.org/proper/commons-lang/download_lang.cgi 下载地址:http://apache.dataguru.cn//commons/lang/binaries/commons-lang3-3.3.2-bin.zip (注:此包可用,低版本的包稳定性更高) commons-lang-2.6-bin http://commons.apache.org/proper/commons-lang/download_lang.cgi?Preferred=http%3A%2F%2Fapache.dataguru.cn%2F 下载地址:http://apache.dataguru.cn//commons/lang/binaries/commons-lang-2.6-bin.zip 第四个包: commons-logging-1.1.3.jar http://commons.apache.org/proper/commons-logging/download_logging.cgi 下载地址:http://apache.dataguru.cn//commons/logging/binaries/commons-logging-1.1.3-bin.zip 第五个包: ezmorph-1.0.2.jar http://ezmorph.sourceforge.net/ http://sourceforge.net/projects/ezmorph/files/ezmorph/ 下载地址:http://nchc.dl.sourceforge.net/project/ezmorph/ezmorph/ezmorph-1.0.6/ezmorph-1.0.6.jar

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值