专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 编程

spring编程-第二一部分,Spring IOC

发布时间:2011-07-01 07:26:28 文章来源:www.iduyao.cn 采编人员:星星草
spring编程----第二部分,Spring IOC
这一部分,主要介绍spring IOC,即spring容器
[][]
第一节.what is Spring IOC?
1.Inversion of Control(IOC)
2.Also calls Dependency Injection(DI)
3.Object art given their dependencies at creation time by some external entity that coordinates each object in the system
4.The key benefit of DI is loose coupling

IOC是整个spring框架的核心,即控制反转,也叫做依赖注入。指的是对象之间的依赖关系,由spring容器在运行时候依据spring的配置文件动态建立。
How to use Spring IOC?
public class Logic{
    Dao dao;//dependency
    public Logic(){
       dao = DaoFactory.getDao();
    }
}


传统上,如果一个类中调用了DaoImpl的方法,需要在这个类中直接创建DaoImpl的对象,或者编写工厂类,有工厂类创建对象。这样的方式都是在编译时就确定了类和DaoImpl的关系,增加了代码的耦合度。

在spring中,我们将对象的创建等相关工作交给spring容器来实现。像web container管理servlet一样,spring容器负责管理JavaBean的生命周期。

IOC的方式
    注入依赖(DI)主要有三种注入方式,即构造器注入、Setter注入和使用注解方式。由于大量的构造器参数可能使程序变得笨拙,特别是党某些属性是可选的时候,因此通常情况下,spring开发团队提倡使用setter注入方式。
    我在下面分别介绍这三种方式
    1.构造器注入方式
先看一个java类
public class StudentService implements IStudentService {   
  
   private IStudentDao studentDao;   
   private String id;   
  
   public StudentService(IStudentDao studentDao,String id){   
    this.studentDao = studentDao;   
    this.id = id;   
   }   
 public void saveStudent() {   
  studentDao.saveStudent();   
  System.out.print(",ID为:"+id);   
 }   
  
}  



在来看一下spring的配置文件
   
      <bean id="studentDao" class="com.wch.dao.impl.StudentDao"></bean>  
 <bean id="studentService" class="com.wch.service.impl.StudentService">  
     <constructor-arg index="0" type="com.wch.dao.IStudentDao" ref="studentDao" />  
     <constructor-arg index="1" value="123456" type="java.lang.String"></constructor-arg>  
 </bean> 
    



基于构造器的DI通过调用带参数的构造器来实现,每个参数代表着一个依赖。在类StudentService 中,有俩个参数,一个是studentDao,一个是id(string类型)用'type'属性来显式指定那些构造参数的类型
index属性来显式指定构造参数的索引,从0开始

setter注入
Spring注入依赖的Setter的方式注入依赖是经常使用的到得一种方法,也非常的简单,只需给每个需要注入的属性设置Setter方法
  构造器注入与Setter注入的比较
     由于大量的构造器参数可能使程序变得笨拙,特别是当某些属性是可选的时候。因此通常情况下,Spring开发团队提倡使用setter注入。而且setter DI在以后的某个时候还可将实例重新配置(或重新注入)
    尽管如此,构造器注入还是得到很多纯化论者(也有很好的理由)的青睐。一次性将所有依赖注入的做法意味着,在未完全初始化的状态下,此对象不会返回给客户代码(或被调用),此外对象也不需要再次被重新配置(或重新注入)。
对于注入类型的选择并没硬性的规定。只要能适合你的应用,无论使用何种类型的DI都可以。对于那些没有源代码的第三方类,或者没有提供setter方法的遗留代码,我们则别无选择--构造器注入将是你唯一的选择

同样是上面的提到的StudentService类,我们来看看setter方式的spring配置文件书写方式
<bean id="studentDao" class="com.wch.dao.impl.StudentDao"></bean>   
  <bean id="studentService2" class="com.wch.service.impl.StudentService2">   
<!--   
     指定需要注入bean       
  -->   
     <property name="studentDao" ref="studentDao"></property>     
<!--    
   直接变量(基本类型、Strings类型等。),可以通过value属性经行赋值    
  -->   
     <property name="id" value="123456789"></property>      
</bean>   



感谢nellybabyhttp://huangliangbao.iteye.com/的blog,省去了我很多的时间。再此歇息

第二节 基本装配
   通过依赖注入注入的信息一般分为3钟情况:
    1.简单值,例如int、spring等
    2.其他bean类型的对象
    3.集合类型

下面来看一个spring的配置文件
<!-- ref方式注入属性 -->
<bean id="personDao" class="com.persia.PersonDaoBean"></bean>
<bean id="personService4" class="com.persia.PersonServiceBean">
  <property name="personDao" ref="personDao"></property>
<property name="name" value="persia"></property>
  <property name="age" value="21"></property>
</bean>


对于简单的值,在<property>标记中使用value即可;如果注入的是其他的bean,需要在property中使用ref

下面来看一个集合类型的spring配置文件
 <property name="sets">
    <!-- 集合的注入 -->
     <set>
       <value>第一个</value>
       <value>第二个</value>
       <value>第三个</value>
     </set>
  </property>
  
  <property name="lists">
    <!-- 集合的注入 -->
    <list>
        <value>第一个l</value>
       <value>第一个l</value>
       <value>第三个l</value>
    </list>
    
  </property>
   
  <property name="map">
   <map>
      <entry key="key1" value="value-1"></entry>
      <entry key="key2" value="value-2"></entry>
      <entry key="key3" value="value-3"></entry>
   </map>
  </property>



如果需要注入的属性类型为list,需要使用<list>标记,允许有重复的元素,而属性类型为set时候,需要使用标记<set>。map类型需要使用标记<map>,它为键值对应

再来看一个
 <property name="properties">
    <props>
      <prop key="key1">value1</prop>
      <prop key="key2">value2</prop>
      <prop key="key3">value3</prop>
    </props>
  </property>
 <property name="properties">
    <props>
        <value>
           key1=value1
           key2=value2
           key3=value3
        </value>
    </props>
  </property>

Properities类型的配置有以上俩种方式
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: