Spring IOC简单例子

来源:互联网 发布:学校机房网络一般布局 编辑:程序博客网 时间:2024/04/30 21:02

Spring框架是一个对Java平台的开源应用框架。Spring框架提供了以下服务:

  • 控制反转容器:配置应用组件和管理Java对象的生命周期
  • 面向切面编程
  • 数据连接
  • 事务管理
  • MVC
  • 远程连接框架
  • Convention-over-configuration
  • Batch processing
  • 认证和授权
  • 远程管理
  • 消息
  • 测试

控制反转(依赖注入)

在Spring框架的中心是控制反转容器,这个容器用Java的反射机制来实现Java对象的配置和管理。容器来管理对象的生命周期,包括创建对象,调用初始化方法等。 在容器中创建的对象也可以叫做Managed ObjectsBeans。通常容器在一个加载的xml文件里配置,这个XML文件包含创建Beans的定义信息。对象可以被所谓的Dependency lookup or Dependency injection得到。Dependency lookup是一个调用者有一个具体的名称或类型来调用容器中对象的模型。Dependency injection是一个容器用名称通过构造器,配置文件或工厂方法来创建一个对象到另一个对象的模型。虽然容器很可能使我们的应用易于配置和个性化,在许多情况下,我们在使用Spring框架的其他部分时并不需要它的容器。Spring容器提供了配置应用和与其它Java环境集成的一致机制,不管小的应用还是大的企业级应用。从注入方法看,主要可以分为:构造函数注入,属性注入和接口注入。Spring支持构造函数注入和属性注入。

简单例子

我们有以下类:

package com.entity;
public interface QuizMaster {
    public String popQuestion();
}

我们有两个类继承了上面的接口:

类1SpringQuizMaster:

package com.entity;
public class SpringQuizMaster implements QuizMaster {
    public String popQuestion() {
        return "Are you new to Spring?";
    }
}

类2StrutsQuizMaster:

package com.entity;
public class StrutsQuizMaster implements QuizMaster {
    public String popQuestion() {
        return "Are you new to Struts?";
    }
}

上面的两个类是实现了接口中的popQuestion方法,一个返回"Are you new to Spring?",另一个返回"Are you new to Struts?"。下面我们用写一个一般的service类来向用户显示问题(popQuestion),

package com.nospring;
import com.entity.QuizMaster;
import com.entity.StrutsQuizMaster;
public class QuizMasterService {
    private QuizMaster quizMaster = new StrutsQuizMaster();
    public void askQuestion(){
        System.out.println(quizMaster.popQuestion());
    }
}

最后我们写一个类在Main方法中调用service:

package com.nospring;
public class QuizProgram {
    /**
     * @param args
     */
    public static void main(String[] args) {
        QuizMasterService quizMasterService = new QuizMasterService();
        quizMasterService.askQuestion();
    }
}

我们可以看到上面的方法很简单,当我们需要打印问题“Are you new to Struts”时,我们需要在QuizMasterService 创建QuizMaster时,QuizMaster quizMaster = new StrutsQuizMaster()。如果我们需要打印问题“Are you new to Spring?”时,需要在在QuizMasterService 创建QuizMaster时,QuizMaster quizMaster = new SpringQuizMaster()。我们可以看到这种代码紧耦合。下面用spring IOC来实现上面的功能。

package com.spring;
import com.entity.QuizMaster;
public class QuizMasterService {
    QuizMaster quizMaster;
    public void setQuizMaster(QuizMaster quizMaster) {
        this.quizMaster = quizMaster;
    }
     public void askQuestion(){
        System.out.println(quizMaster.popQuestion());
    }
}

我们在QuizMasterService中定义了quizMaster对象并写了一个对此对象的set方法。 我们在使用Spring时可以用构造注入或SET注入,这里我们用了SET注入。下面是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="springQuizMaster" class="com.entity.SpringQuizMaster"></bean>
   <bean id="strutsQuizMaster" class="com.entity.StrutsQuizMaster"></bean>
   <bean id="quizMasterService" class="com.spring.QuizMasterService">
      <property name="quizMaster">
        <ref local="strutsQuizMaster"/>
      </property>
   </bean>
</beans>

下面我们调用service方法:

package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class QuizProgram {
    /**
     * @param args
     */
    public static void main(String[] args) {
        /*A couple of details about this class:
        * The ApplicationContext interface helps us to plug into the Spring container and
        * lookup the classes we need by using the name we defined in the xml file.
        * There are many implementataions of ApplicationContext available in Spring.
        * One of the most widely used implementation is “ClassPathXmlApplicationContext”.
        * It is used to load Spring configuration files found in classpath.
        */
        ApplicationContext context = new ClassPathXmlApplicationContext("com/resources/beans.xml");
        QuizMasterService quizMasterService = (QuizMasterService) context.getBean("quizMasterService");
        quizMasterService.askQuestion();
    }

}

这样如果我们想打印问题“Are you new to Spring?”时,只需在配置文件中修改:

      <property name="quizMaster">
        <ref local="springQuizMaster"/>
      </property>

这样我们可以看到代码之间没有那么多的耦合,我们只需在配置文件中对想显示的内容进行修改即可。

下面是代码整体架构:


参考:

http://www.vaannila.com/spring/spring-ioc-1.html

http://en.wikipedia.org/wiki/Spring_IOC_Framework

原创粉丝点击