Sunday, May 18, 2014

Spring notes

Bean initialization 

package com.springinaction.knights;
public class BraveKnight implements Knight {
  private Quest quest;
public BraveKnight(Quest quest) {
  this.quest = quest;
}
public void embarkOnQuest() throws QuestException {
quest.embark();
}



package com.springinaction.knights;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class KnightMain {
  public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("knights.xml");

    Knight knight = (Knight) context.getBean("knight");
knight.embarkOnQuest();
}

knights.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="knight" class="com.springinaction.knights.BraveKnight">
<constructor-arg ref="quest" /> 
</bean>
<bean id="quest" class="com.springinaction.knights.SlayDragonQuest" />

</beans>


Application Context

  • ClassPathXmlApplicationContext—Loads a context definition from an XML file located in the classpath, treating context definition files as classpath resources.
  • FileSystemXmlApplicationContext—Loads a context definition from an XML file in the file system.
  • XmlWebApplicationContext—Loads context definitions from an XML file con- tained within a web application.

Mock Test

package com.springinaction.knights;
import static org.mockito.Mockito.*;
import org.junit.Test;

public class BraveKnightTest {
  @Test

  public void knightShouldEmbarkOnQuest() throws QuestException {

Quest mockQuest = mock(Quest.class);
    BraveKnight knight = new BraveKnight(mockQuest);
    knight.embarkOnQuest();
    verify(mockQuest, times(1)).embark();
  }


Factory instance

package com.springinaction.springidol;
        public class Stage {
          private Stage() {
          }

private static class StageSingletonHolder {
    static Stage instance = new Stage();
}
  public static Stage getInstance() {
    return StageSingletonHolder.instance;
} }
 


The <bean> element has a factory-method attribute that lets you specify a static method to be invoked instead of the constructor to create an instance of a class. To configure Stage as a bean in the Spring context, you simply use factory- method as follows:
<bean id="theStage" class="com.springinaction.springidol.Stage" factory-method="getInstance" />



create new instance for each bean

<bean id="ticket" class="com.springinaction.springidol.Ticket" scope="prototype" />

init and destroy bean

bean id="auditorium" class="com.springinaction.springidol.Auditorium" init-method="turnOnLights"

     destroy-method="turnOffLights"/>
 
Innerbean initialization 
 
<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>

  
<bean id="duke" class="com.springinaction.springidol.PoeticJuggler">
  <constructor-arg value="15" />
  <constructor-arg>
<bean class="com.springinaction.springidol.Sonnet29" /> </constructor-arg>
</bean>
 
Spring's p namespace


<bean id="kenny" class="com.springinaction.springidol.Instrumentalist" p:song = "Jingle Bells"
      p:instrument-ref = "saxophone" />
 
Collections
 
<bean id="hank"
class="com.springinaction.springidol.OneManBand">

     
  <property name="instruments">
    <list>
      <ref bean="guitar" />
      <ref bean="cymbal" />
      <ref bean="harmonica" />
    </list>
  </property>
</bean>
 
<bean id="hank"
class="com.springinaction.springidol.OneManBand">

     
  <property name="instruments">
    <set>
      <ref bean="guitar" />
      <ref bean="cymbal" />
      <ref bean="harmonica" />
      <ref bean="harmonica" />
    </set>
  </property>
</bean> 

An <entry> in <map> is made up of a key and a value

key Specifies the key of the map entry as a String
key-ref
Specifies the key of the map entry as a reference to a bean in the Spring context 

value Specifies the value of the map entry as a String
value-ref
Specifies the value of the map entry as a reference to a bean in the Spring context
 

<bean id="hank" class="com.springinaction.springidol.OneManBand"> <property name="instruments">
    <map>
      <entry key="GUITAR" value-ref="guitar" />
      <entry key="CYMBAL" value-ref="cymbal" />
      <entry key="HARMONICA" value-ref="harmonica" />
    </map>
  </property>
</bean> 
 

<bean id="hank" class="com.springinaction.springidol.OneManBand"> <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>
</bean> 

null property

<property name="someNonNullProperty"><null/></property>


No comments:

Post a Comment