spring wiring beans

 

In Spring, objects aren’t responsible for finding or creating the other objects that they need to do their jobs. Instead, they’re given references to the objects that they collaborate with by the container.

 

Spring is a container-based framework. But if you don’t configure Spring, then it’s an empty container and doesn’t serve much purpose. 

 

1.通过构造函数:定义一个bean

  • <bean id="duke" class="com.springinaction.springidol.Juggler" /> //使用默认构造函数
  • 指明构造函数:参数列表顺序严格按照构造函数中的顺序
<bean id="duke"class="com.springinaction.springidol.Juggler">
    <constructor-arg value="15" />
</bean>
  •  
    <bean id="poeticDuke"
    class="com.springinaction.springidol.PoeticJuggler">
        <constructor-arg value="15" />
        <constructor-arg ref="sonnet29" />
    </bean>
     

2.通过工厂方法:定义一个bean

  • <bean id="theStage" class="com.springinaction.springidol.Stage"
        factory-method="getInstance" />
     

3.bean作用域:

  • singleton:单例,默认的类型
  • prototype:每次需要时生成新的
  • request:
  • session:
  • global-session:
  • <bean id="ticket"
    class="com.springinaction.springidol.Ticket" scope="prototype" />
     

4.bean初始化:

  • init-method
  • 实现 InitializingBean 接口
    public interface InitializingBean
    {
    	
    	void afterPropertiesSet() throws Exception;
    }
     

5.bean销毁:

  • destroy-method
  • 实现DisposableBean 接口

6.定义全局的初始化和销毁函数:在Beans标签中定义

  • default-init-method
  • default-destroy-method

 7.注入属性

  • 注入简单的值:可以是String,数值类型:int、float、double,布尔值;注入的方式都是以字符串的形式,spring会根据属性的类型,转换成相应的类型
  • <property name="song" value="Jingle Bells" />
    <property name="age" value="37" />
  •  注入bean:
    <property name="instrument" ref="saxophone" />
  •  注入内部bean:
    <property name="instrument">
        <bean class="org.springinaction.springidol.Saxophone" />
    </property>
     在构造函数中也可以使用:顺序还是依据构造函数定义的来
    <constructor-arg>
        <bean class="com.springinaction.springidol.Sonnet29" />
    </constructor-arg>
  •  注入集合
  • spring wiring beans_第1张图片
    list和set可以是任何collection的实现类,甚至是 数组;
    <property name="instruments">
        <list>
            <ref bean="guitar" />
            <ref bean="cymbal" />
            <ref bean="harmonica" />
        </list>
    </property>
    
    <map>
        <entry key="GUITAR" value-ref="guitar" />
        <entry key="CYMBAL" value-ref="cymbal" />
        <entry key="HARMONICA" value-ref="harmonica" />
    </map>
    
    <property name="instruments">
        <props>
            <prop key="GUITAR">STRUM STRUM STRUM</prop>
            <prop key="CYMBAL">CRASH CRASH CRASH</prop>
            <prop key="HARMONICA">HUM HUM HUM</prop>
         </props>
    </property>
  • 注入null值:
    <property name="someNonNullProperty"><null/></property>
     

 8.p 命名空间

  • URI :http://www.springframework.org/schema/p ;xmlns:p="http://www.springframework.org/schema/p",看例子:
    <bean id="kenny" class="com.springinaction.springidol.Instrumentalist"
      p:song = "Jingle Bells"
      p:instrument-ref = "saxophone" />
     

 

你可能感兴趣的:(spring)