Spring study guide - about Spring

来源:互联网 发布:linux utf8转ansi 编辑:程序博客网 时间:2024/05/19 06:46

1. The advantage of Spring

 a. Unleashing the power of POJOs

 b. Injecting dependencies

 c. Applying aspects

 d. Eliminating boilerplate code with templates

2. A bean's life cycle

 

1 Spring instantiates the bean.
2 Spring injects values and bean references into the bean’s properties.
3 If the bean implements BeanNameAware, Spring passes the bean’s ID to the set-BeanName() method.
4 If the bean implements BeanFactoryAware, Spring calls the setBeanFactory() method, passing in the bean factory itself.

5 If the bean implements ApplicationContextAware, Spring will call the set-ApplicationContext() method, passing in a reference to the enclosing appli-
cation context.

6 If any of the beans implement the BeanPostProcessor interface, Spring callstheir postProcessBeforeInitialization() method.
7 If any beans implement the InitializingBean interface, Spring calls their afterPropertiesSet() method. Similarly, if the bean was declared with an init-method, then the specified initialization method will be called.
8 If there are any beans that implement BeanPostProcessor, Spring will call their postProcessAfterInitialization() method.
9 At this point, the bean is ready to be used by the application and will remain in the application context until the application context is destroyed.
10 If any beans implement the DisposableBean interface, then Spring will call their destroy() methods. Likewise, if any bean was declared with a destroy-method, then the specified method will be called.

3. Declaring beans

   a.  Simple form

        <bean id="sonnet29" class="com.springinaction.springidol.Sonnet29"/>

   b.  By Constructor

       <bean id="poeticDuke" class="com.springinaction.springidol.PoeticJuggler">
                <constructor-argvalue="15"/>
                <constructor-argref="sonnet29"/>
       </bean>

   c.   By factory method

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

        Create the singleton instance with "initialization on demand holder"

        public class Stage {
          private Stage() {
          }

          private static class StageSingletonHolder {
            static Stage instance = new Stage();
          }

         public static Stage getInstance() {
            return StageSingletonHolder.instance;
         }
       }

    d. By Setter method

        <bean id="kenny" class="com.springinaction.springidol.Instrumentalist">
          <propertyname="song"value="JingleBells"/>
        </bean>

    e.  Inner Bean  

       <bean id="kenny" class="com.springinaction.springidol.Instrumentalist">

           <property name="song" value="Jingle Bells" /> 

          <property name="instrument"> 

               <bean class="org.springinaction.springidol.Saxophone" /> 

          </property> 

       </bean> 

4. Bean Scope

 

5.  Initializing and destroying beans

   <bean id="auditorium"  class="com.springinaction.springidol.Auditorium"
       init-method="turnOnLights"  destroy-method="turnOffLights"/>

6. Wiring collectioins

    

Either <list> or <set> can be used to wire any implementation of java.util.Collection or an array. Just because a property is a java.util.Set, that doesn’t mean that you must use<set>to do the wiring. Even though it may seem odd to configure ajava.util.Listproperty using <set>, it’s certainly possible. In doing so, you’ll be guaranteed that all members of theListwill be unique. 

7.  SPEL

 a. invoke another bean's property

<bean id="carl" class="com.springinaction.springidol.Instrumentalist">

 <property name="song" value="#{kenny.song}" /> 

</bean> 

 b. invoke another bean's method

<property name="song" value="#{songSelector.selectSong().toUpperCase()}"/> 

 c. working with types

<property name="multiplier" value="#{T(java.lang.Math).PI}"/> 

<property name="randomNumber" value="#{T(java.lang.Math).random()}"/> 

d. performing operations on SpEL values




<property name="song" value="#{kenny.song ?: 'Greensleeves'}"/> is equivalent to

<property name="song" value="#{kenny.song != null ? kenny.song : 'Greensleeves'}"/> 


0 0