码迷,mamicode.com
首页 > 编程语言 > 详细

spring——控制反转简单例子

时间:2015-12-21 09:20:09      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

 spring框架是一个开源的轻量级的基于IOC与AOP核心技术的容器框架,主要是解决企业的复杂操作实现。

   那IOC与AOP,到底如何解释呢,在看spring视频中,两个专业术语一定必须要懂得。

       IOC:inverse of Control:控制反转。意思是程序中的之间的关系,不用代码控制,而完全是由容器来控制。在运行阶段,容器会根据配置信息直接把他们的关系注入到组件中。同样,这也是依赖注入的含义。依赖注入和控制反转其实是一个概念。只不过强调的不同而已,依赖注入强调关系的注入是由容器在运行时完成,而控制反转强调关系是由容器控制。其实本质是一样的。

   用代码演示一下控制反转是如何实现的。

1。新建一个普通的java项目。

2。引入相应的jar包。Spring.jar,log4j-1.2.14.jar(提供日志功能的),commons-logging.jar

3。提供log4j.properties配置文件。(日志jar包可以不用添加)

4。提供配置文件ApplicationContext.xml文件

5。开始写代码。

  1. package ioc.iocsample;  
  2. /** 
  3.  * 学校类 
  4.  * @author lhy 
  5.  * 
  6.  */  
  7. public class School {  
  8.    private String name;  
  9.    public School(String name)  
  10.    {  
  11.        this.name=name;  
  12.    }  
  13.    public void printInfo()  
  14.    {  
  15.        System.out.println("该学校的名称是:"+name);  
  16.    }  
  17. }  
  1. package ioc.iocsample;  
  2. /** 
  3.  * 学生类 
  4.  * @author lhy 
  5.  * 
  6.  */  
  7. public class Student {  
  8.   public int id;  
  9.   public String name;  
  10.   private School school;  
  11. public int getId() {  
  12.     return id;  
  13. }  
  14. public void setId(int id) {  
  15.     this.id = id;  
  16. }  
  17. public String getName() {  
  18.     return name;  
  19. }  
  20. public void setName(String name) {  
  21.     this.name = name;  
  22. }  
  23. public School getSchool() {  
  24.     return school;  
  25. }  
  26. public void setSchool(School school) {  
  27.     this.school = school;  
  28. }    
  29. }  

 
     配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xmlns:tx="http://www.springframework.org/schema/tx"  
  7.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  10.  <bean id="school" class="ioc.iocsample.School">  
  11.        <constructor-arg index="0">  
  12.          <value>廊坊师院</value>  
  13.        </constructor-arg>  
  14.  </bean>  
  15.  <bean id="student" class="ioc.iocsample.Student">  
  16.      <property name="id"      value="001"/>  
  17.      <property name="name" value="张三"/>  
  18.      <property name="school"  ref ="school"/>  
  19.  </bean>  
  20. </beans>  


    客户端进行测试,其中学生类Student中有School中的引用,测试该学生就读的学校的名称如下:

  1. package ioc.iocsample;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Client {  
  7.     public static void main(String[] args) {  
  8.         // TODO Auto-generated method stub  
  9.         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");  
  10.         Student student=(Student)factory.getBean("student");  
  11.         student.getSchool().printInfo();  
  12.     }  
  13. }  


    其中,在程序中不用实例化学生类,学校类,直接由容器中的beanFactory直接创建,隐藏了创建了细节。同时,程序中也不用关心学生类与学校类之间的依赖关系,而由容器来进行负责,在运行的时候,容器会把属性值及依赖关系注入学生类和学校类中的javabean中(其实在此School和Student就是一个javaBean。javaBean就是一个按照一定的原则封装的java类而已。)

   其中依赖注入包括两种:一种赋值注入(使用getter和setter方法);另一种使用构造器注入。

   看程序中student中的ID,name都是使用get,set来赋值的:那在配置文件是如下配置:

       <property name="id"      value="001"/>
       <property name="name" value="张三"/>

   并且Student中的school属性是School类型,则在容器中是如下配置的:

       <property name="school"  ref ="school"/>

   而在程序中的School中的name是构造器赋值的,则容器中是如下配置的:

      <constructor-arg index="0">
         <value>廊坊师院</value>
      </constructor-arg>

   构造器中一个参数,则索引值是从0开始,若是有多个,依次递增。

    若构造器中的是一个类,则使用bean标签

   <constructor-arg index="0">

    <bean class="具体的类">

  </constructor-arg>

  

   spring中的依赖注入DI(dependence injection)共有三种方式:第一种是接口注入(Interface Injection)第二种是get set注入(set/get Injection)第三种是构造器注入(Constructor Injection)

   三种注入方式的区别:

   1.接口注入:组件需要依赖特定接口的实现,其中的加载接口实现和接口实现的具体对象都是由容器来完成。这样,接口必须依赖容器,这样的组件具有侵入性,降低了重用性。其中如J2EE开发中常用的Context.lookup(ServletContext.getXXX),都是接口注入的表现形式。(这种注入方式不是常用的)

  

   2.getter/setter方式注入:对于需要注入的东西比较明确。符合java的设计规则。更适合java开发人员,使用起来更加自然,更加方便。

 

   3.构造器方式注入:在类加载的时候,就已经注入依赖的组件。但是若是参数多的话,使用起来不方便。

 

  但是后两种注入方式是spring常用的,而第一种接口注入方式不常用。

spring——控制反转简单例子

标签:

原文地址:http://www.cnblogs.com/ChrisMurphy/p/5062444.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!