Spring 控制反转IOC(依赖注入,)

<?xml version="1.0" encoding="gb2312"?>

<!-- default-lazy-init="true" 为beans下所有的进行延迟初始化 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >
       <!-- id 不能是特殊字符,如:“/”,如果有特殊字符则应该用name。      交给Spring管理的Bean类-->
      
       <!--默认情况下(没有配置scope属性)是单实例  指的是每次获取Bean都是同一个对象  初始化容器时候就实例化实例化 PersonService
          可是使用lazy-init="true" 进行延迟初始化 ,即第一次获取bean的时候才初始化
           scope="prototype"每次获取Bean都是新的对象   第一次请求(调用getBean)的时候实例化 PersonService  .
           init-method="" 初始化 方法。即当bean被初始化后接下来会继续执行的方法。
           destroy-method=""  -->
          
      <!--  <bean id="personService" class="com.milo.services.impl.PersonServiceBean" 
         lazy-init="false" init-method="init" destroy-method="destory" scope="prototype"/>     -->
      
      
       <!-- 使用 类构造器方法        实例化 PersonService   
        <bean id="personService1" class="com.milo.services.impl.PersonServiceBean" scope="prototype"/>
      
       使用 静态工厂方法        实例化PersonService 
       <bean id="personService2" class="com.milo.services.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBean"/>
      
       使用 实例化工厂方法   实例化PersonService 
       <bean id="personServiceFactory" class="com.milo.services.impl.PersonServiceBeanFactory"/>
       <bean id="personService3" factory-bean="personServiceFactory" factory-method="createPersonServiceBean2"/>
  -->
  
  
  <!-- 依赖对象注入   可以被多个Bean调用-->
  <bean id="personDao" class="com.milo.dao.impl.PersonDaoBean"></bean>  <!-- 默认被实例化 -->
  <bean id="personService" class="com.milo.services.impl.PersonServiceBean" >
    <!-- ref="personDao" 注入实例 -->
   <property name="personDaoTest" ref="personDao"></property>
   
    <!--对基本类型的注入  -->
   <property name="name" value="milo.yang"></property>
   <property name="id" value="88"></property>
   
   <!-- 对集合类的注入操作 -->
   <property name="sets">
    <set>     <!-- Set 集合注入 -->
     <value>第1个set</value>
     <value>第2个set</value>
     <value>第3个set</value>
    </set>
   </property>
   <property name="lists">
    <list>    <!-- List 集合注入 -->
     <value>第1个list</value>
     <value>第2个list</value>
     <value>第3个list</value>
    </list>
   </property>
   <property name="props">
    <props>    <!-- Properties 集合注入 -->
     <prop key="propsKey1">第1个properties</prop>
     <prop key="propsKey2">第2个properties</prop>
     <prop key="propsKey3">第3个properties</prop> 
    </props>
   </property>
   <property name="maps">
    <map>    <!-- Map 集合注入 -->
     <entry key="mapsKey1" value="第1个maps"></entry>
     <entry key="mapsKey2" value="第2个maps"></entry>
     <entry key="mapsKey3" value="第3个maps"></entry>
    </map>
   </property>
   
   
  </bean>
  
  <!-- 也可以采用内部Bean的方式注入对象
   但不可以为其他Bean调用,只能被PersonServiceBean使用。
  <bean id="personService" class="com.milo.services.impl.PersonServiceBean" >
   <property name="personDaoTest" >
    <bean class="com.milo.dao.impl.PersonDaoBean"/>
   </property>
  </bean>
   -->
  
  
</beans>

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值